Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Read a variable backwards..

by sathya83aa (Acolyte)
on Jan 06, 2014 at 12:01 UTC ( #1069494=perlquestion: print w/replies, xml ) Need Help??
sathya83aa has asked for the wisdom of the Perl Monks concerning the following question:

Hi all..

I have a variable in perl which has some 20 numbers substr from a file. The data in the variable looks like


What I want to do is to read the data in the variable backwards character by character. To elaborate I should read the above mentioned line as 5 first, 3 second, 8 third and so on.

Any help on this is appreciated.

Thanks & Regards,

Sathya V.

Replies are listed 'Best First'.
Re: Read a variable backwards..
by choroba (Bishop) on Jan 06, 2014 at 12:03 UTC
    See reverse on how to reverse a string. See split on how to split a string into characters.
    my $var = "8940163...835"; for my $char (split //, reverse $var) { print "$char\n" }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Read a variable backwards..
by Utilitarian (Vicar) on Jan 06, 2014 at 12:04 UTC
    $ perl -E '$string="esrever"; say join("",reverse(split(//,$string))); +' reverse
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Read a variable backwards..
by johngg (Abbot) on Jan 06, 2014 at 13:03 UTC

    You could use chop if you don't need to retain the string.

    $ perl -E ' $str = q{abcdefg}; say chop $str while $str;' g f e d c b a $

    I hope this is helpful.



      check this code

      $str="123456789"; @arr=split(//,$str); while($tmp=pop(@arr)){ print "\nfirst-frm last-->$tmp"; }
Re: Read a variable backwards..
by Random_Walk (Prior) on Jan 06, 2014 at 12:11 UTC
    my $var = "11223344556677889900"; for (1..length $var) { print substr $var, -$_, 1; # or do anything else with it }
    or as a one liner perl -E '$v="11223344556677889900";say substr $v, -$_, 1 for (1..length $v)'


    Pereant, qui ante nos nostra dixerunt!
Re: Read a variable backwards..
by ww (Archbishop) on Jan 06, 2014 at 12:23 UTC
Re: Read a variable backwards..
by kcott (Chancellor) on Jan 06, 2014 at 12:41 UTC

    G'day sathya83aa,

    I see a number of solutions involving breaking the string into individual characters and then recombining in reverse order. This isn't necessary as reverse in scalar context will do this for you.

    When the context is already scalar, you can just use the reverse function:

    $ perl -le 'my $x = "8940163...835"; my $y = reverse $x; print $y' 538...3610498

    In list context, you can force scalar context with the scalar function:

    $ perl -le 'my $x = "8940163...835"; print scalar reverse $x' 538...3610498

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069494]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-02-18 23:35 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.