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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to cut strings from the end
by Zaxo (Archbishop) on May 30, 2006 at 11:07 UTC

    You want substr.

    my $string = 'perl345'; my $tag = substr $string, -3;
    Or if you want to remove the last three characters, too,
    my $string = 'monks 1223'; my $tag = substr $string, -3, 3, '';

    After Compline,
    Zaxo

Re: How to cut strings from the end
by prasadbabu (Prior) on May 30, 2006 at 11:03 UTC

    Hi Bharath,

    You have to take a look at substr or you can use regex to accomplish your need. Please do Super Search before posting.

    $string =~ s/.{3}$//;

    Prasad

Re: How to cut strings from the end
by japhy (Canon) on May 30, 2006 at 11:05 UTC
    You can use substr() to extract a substring from a string. You'll probably want to use the two-argument version (see the docs).

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to cut strings from the end
by b10m (Vicar) on May 30, 2006 at 11:08 UTC

    Of course the "right answer" is already given, yet if you want to go crazy, you might also abuse split for this ;-)

    my $string = 'monk1223'; print ((split "", $string)[$#a-2..$#a]);
    --
    b10m

    All code is usually tested, but rarely trusted.

      Could you explain how this works? I was trying these days to dismiss the first element of an anonymous list, then I got this code:

      (undef, @list) = split /\s+/, $str;
      I was wondering if something like that could work, but it doesn't:
      @list = (split /\s+/, $str)[1..$#a];
      Thanks in advance.

      Igor 'izut' Sutton
      your code, your rules.

        His code assumes @a is empty, which means $#a returns -1, so he's doing (...)[-1 - 2 .. -1] which is just (...)[-3 .. -1]. Your case cannot be handled in a similar manner -- you'd need to know the size of the list being returned by split(). Your first approach is fine.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to cut strings from the end
by polettix (Vicar) on May 30, 2006 at 11:09 UTC
    The matter has been covered recently: substring problem???. Only, in that occasion the OP needed the last four characters in the string ;)

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: How to cut strings from the end
by chargrill (Parson) on May 31, 2006 at 02:34 UTC

    This will do want you want:

    $string = 'perl345'; $_ = $string; chop; chop; chop; #! $string = $_;

    Unless I have the sense of your question reversed, in which case this will work:

    $string = "perl345"; for( reverse split //, $string ){ $newstring .= chop unless /[a-z]/ } print scalar reverse $newstring;

    ;-)



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}