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

reversing substr

by DaWolf (Curate)
on Mar 11, 2002 at 04:28 UTC ( [id://150809]=perlquestion: print w/replies, xml ) Need Help??

DaWolf has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, fellow monks.

I have a question that's probably not original but I was unable to find it on supersearch.

I know that if you put the following code:
my $str = "1234"; my $ss = substr ($str,2); print $ss;
will return 34

Well, I need the opposite, meaning a substr that cuts the string from the right, not from the left...

I've tried entering a negative offset - sounds logic to me - but that doesn't work...

So, what's the trick?

Thanks in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
Re: reversing substr
by abstracts (Hermit) on Mar 11, 2002 at 04:41 UTC
    Hello,

    What you want to do probably falls into one of these cases:

    # dashes are just for illustration my $str = "123456789"; my $ss = substr ($str,3); print "$ss\n"; # will print ---456789 $ss = substr ($str,-3); print "$ss\n"; # will print ------789 $ss = substr ($str,0,3); print "$ss\n"; # will print 123------ $ss = substr ($str,0,-3); print "$ss\n"; # will print 123456---
    Check out perlfunc substr to know more about it since this function in particular has a very informative description with lots of examples.

    Hope this helps,,,
    Aziz,,,

Re: reversing substr
by chipmunk (Parson) on Mar 11, 2002 at 04:43 UTC
    It's not quite clear what you're trying to do, especially since substr does accept a negative offset. What do you mean when you say "that doesn't work"?

    If you want the first two characters of the string, try this:

    my $str = "12345"; my $ss = substr($str, 0, 2); print $ss;
    Or, if you want all but the last two characters, try this:
    my $str = "12345"; my $ss = substr($str, 0, -2); print $ss;
Re: reversing substr
by talexb (Chancellor) on Mar 11, 2002 at 04:33 UTC
    Check out perlfunc:substr for information on how substr works -- you can find it on this site by typing in 'substr' in the search field at the top left of your screen.

    --t. alex

    "There was supposed to be an earth-shattering kaboom!" --Marvin the Martian

Re: reversing substr
by davis (Vicar) on Mar 11, 2002 at 09:28 UTC
    Hi there,
    I'm a little unsure as to what you're asking, but if you're asking for the substring you create to be reversed, you can use reverse thusly:
    my $str = "1234"; my $ss = reverse substr($str, 2); print $ss;
    Hope that helps,
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re: reversing substr
by jlongino (Parson) on Mar 11, 2002 at 16:47 UTC
    If you're looking for something truncate the string, then you might want to look into the pack function. It has the added advantage of right-padding the string with blanks:
    use strict; my $str = "1234"; my $ss = pack "A2", $str; print "'$ss'\n"; ### example shows example of padding blanks $ss = pack "A5", $str; print "'$ss'\n";
    Produces:
    '12' '1234 '

    --Jim

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found