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

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

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.

Comment on Read a variable backwards..
Re: Read a variable backwards..
by choroba (Abbot) 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 Random_Walk (Parson) 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 (Bishop) on Jan 06, 2014 at 12:23 UTC
Re: Read a variable backwards..
by kcott (Abbot) 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

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.

    Cheers,

    JohnGG

      check this code

      $str="123456789"; @arr=split(//,$str); while($tmp=pop(@arr)){ print "\nfirst-frm last-->$tmp"; }

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-22 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls