Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Read a variable backwards..

by sathya83aa (Acolyte)
on Jan 06, 2014 at 12:01 UTC ( [id://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

8940163...835

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 (Cardinal) 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 (Canon) 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.

    Cheers,

    JohnGG

      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)'

    Cheers,
    R.

    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 (Archbishop) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1069494]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found