in reply to reverse is not reversing

Print is imposing a list context.

You need scalar context for string reverse

print scalar reverse $test;

What's happening is that the one element list is reversed now, which is of course useless.

DB<5> print reverse qw/a1 b2 c3/ # inverting list c3b2a1 DB<6> print scalar reverse qw/a1 b2 c3/ # inverting string 3c2b1a

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

added code

Replies are listed 'Best First'.
Re^2: reverse is not reversing
by badbanana (Novice) on Nov 26, 2018 at 15:17 UTC
    oh. it only works with more than one word. i thought it would reverse "apple" into "elppa".
      No, it will reverse "apple" into "elppa", but only in scalar context. Read more about context in the book Modern Perl.

      Update: Fixed the book title. Thanx LanX.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        > in the book Moder Perl

        Molder Perl? ;-p

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      > it only works with more than one word

      that's wrong, context matters (LHS) not input (RHS)

      DB<9> print reverse "text" # list context text DB<10> print scalar reverse "text" # scalar context txet DB<11> $a= reverse "text" # scalar DB<12> print $a # still print LIST, but $a is +already reversed txet

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice