Perl: Why can't divide?

Related searches

Why divide is not working?

echo 'cb0' | perl -p -e 's/(\w{3})/sprintf("%d", ( hex($1)/4 ))/e'

Number found where operator expected at -e line 1, near
"s/(\w{3})/sprintf("%d", ( hex($1)/4" syntax error at -e line 1, near
"s/(\w{3})/sprintf("%d", ( hex($1)/4" Execution of -e aborted due to
compilation errors.

Raw input data is 12 bits hex number.


The default delimiter for substitution s/// is not balanced, so you get the error. This is because you are dividing the $1 by 4 and it introduced another '/' . So to be safe use some other character other than the one that would mess with the expression. In this case better use <> as delimtier

$ echo 'cb0' | perl -p -e 's<(\w{3})><sprintf("%d", ( hex($1)/4 ))>e '
812

when you convert s/// with other delimiters, you have to pair <...>, (...), [...] and {...}. I chose <> as it is not there in the expression.

perlobj, This includes methods inherited or imported by $obj , CLASS , or VAL . can cannot know whether an object will be able to provide a method through AUTOLOAD,� Perl runs basic CGI and is the language that made CGI ubiquitous on the web. mod_perl is an advanced implementation of Perl that runs in the Apache web server. It provides extremely fast performance and full access to Apache internals via Perl.


This doesn't work:

$ echo 'cb0' | perl -p -e 's/(\w{3})/sprintf("%d", ( hex($1)/4 ))/e'
Unknown regexp modifier "/4" at -e line 1, at end of line
syntax error at -e line 1, near "s/(\w{3})/sprintf("%d", ( hex($1)/4 )"
Execution of -e aborted due to compilation errors.

This does:

$ echo 'cb0' | perl -p -e 's/(\w{3})/sprintf("%d", ( hex($1)\/4 ))/e'
812

So does this, and it might be easier to follow:

$ echo 'cb0' | perl -p -e 's|(\w{3})|sprintf("%d", ( hex($1)/4 ))|e'
812

When you use a / character as the separator character in a substitution, then all occurrences of / in your expression will be seen as separators. If you want one to be see as something else (a division operator, for example) you need to escape it (by putting \ in front of it).

Another option is to choose a different separator for your substitution. In my third example, I've switched to using | instead. In this case, the / is no longer seen as special and doesn't need to be escaped.

perldiag, In the simplest case, the $class variable will end up containing the string "File". We can also use a variable to store a reference to the data structure that is being � Perl is a highly capable, feature-rich programming language with over 30 years of development. Perl runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. "Perl" is a family of languages, "Raku" (formerly known as "Perl 6") is part of the family, but it is a separate language which has its own development team.


Either use a different delimiter for the substitution or you will need to escape the divide with a backslash:

echo 'cb0' | perl -p -e 's/(\w{3})/sprintf("%d", ( hex($1)\/4 ))/e'

perlfaq3, If a message can be controlled by the warnings pragma, its warning category is Perl will not try to compile this file again unless you delete its entry from %INC. Nearly unlimited - for high-level programming (no real time, no kernel, no device drivers please) Mostly fast - fast enough for the tasks you would do with Perl Can solve problems - the symbol of Perl is the camel, ugly but can handle difficult environment


perl -de 42. Now just type in any legal Perl code, and it will be immediately evaluated. You can also examine the symbol table, get stack� Perl used to be the most popular web programming language due to its text manipulation capabilities and rapid development cycle. Perl is widely known as "the duct-tape of the Internet". Perl can handle encrypted Web data, including e-commerce transactions. Perl can be embedded into web servers to speed up processing by as much as 2000%.


No one has said there can’t be, but in the jump to Perl 7, the developers don’t want to redo what’s already working. This change should be manageable with as few side quests as possible. Also, PAUSE, the Perl Authors Upload Server, has received quite a bit of love in the past couple of years.


In Perl, array is a special type of variable.The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable.