Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: Binary to decimal conversion

by Fletch (Chancellor)
on Dec 11, 2007 at 13:43 UTC ( #656405=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Binary to decimal conversion
in thread Binary to decimal conversion

Erm . . . a LIST may consist of a single scalar value. That single scalar value is concatenated with all the other elements in the LIST (very quickly, obviously, since they're non-existent) and the bytes in that concatenated string (i.e. the single scalar argument) are returned in reverse order, just like the docs say.

The cake is a lie.
The cake is a lie.
The cake is a lie.


Comment on Re^5: Binary to decimal conversion
Re^6: Binary to decimal conversion
by andreas1234567 (Vicar) on Dec 11, 2007 at 13:57 UTC
    Then tell me why this prints hello and not olleh?
    $ perl -wle 'print reverse(q{hello})' hello
    --
    Andreas

      Because print takes a LIST and hence provides a list context to reverse which triggers the list context behavior from the latter (and a 1 element list reversed is indistinguishable from the original 1 element list for obvious reasons). Stick a scalar in there after the print, and/or store the reverse into a scalar temporary variable, and you'll see the behavior you're expecting.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Said code (and then some):
        $\ = "\n"; print(my @a = reverse('hello')); # hello (order or scalars reversed) print(my $s = reverse('hello')); # olleh (order of chars reversed) print(reverse('hello')); # hello (order or scalars reversed) print(scalar(reverse('hello'))); # olleh (order of chars reversed) sub f(@) { return @_; } sub g($) { return @_; } print(f(reverse('hello')); # hello (order or scalars reversed) print(g(reverse('hello'))); # olleh (order of chars reversed)
      Because you did not RTFM :)
      reverse LIST In list context, returns a list value consisting of the elemen +ts of LIST in the opposite order. In scalar context, concatenates the elements of LIST and returns a string value with all characters in the opposite order. print reverse <>; # line tac, last line first undef $/; # for efficiency of <> print scalar reverse <>; # character tac, last line tsr +if Used without arguments in scalar context, reverse() reverses $ +_. This operator is also handy for inverting a hash, although the +re are some caveats. If a value is duplicated in the original has +h, only one of those can be represented as a key in the inverted hash. Also, this has to unwind one hash and build a whole new one, which may take some time on a large hash, such as from a DBM file. %by_name = reverse %by_address; # Invert the hash

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://656405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-07-04 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls