cdherold 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: Variable Removal From End of String
by jeroenes (Priest) on May 01, 2001 at 11:18 UTC
    Your post is not really clear. Do you mean Array or String?

    substr( $str, -20 )= ''; #string, removes chars splice( @array, -20 )= (); #array, removes scalars

    Jeroen
    "We are not alone"(FZ)
    Update: Avoided double numbers by using the lvalue property of these functions.

      Apologies for the lack of clarity in the post (still new to this game)...

      I have an array ,@images, that I made into a scalar by the following method (my mistake on terming this a string if it is not) ...

      $links = join(",", @img), "\n";

      When I print $links out I get a list (a,b,c...etc). The size of this list is variable, but I wish to get rid of the last 20 components in every case.

      I tried to use the splice () option you suggested before joining @img into a scalar. I had compilation problems, however. More than likely I am doing it incorrectly ...

      Could you let me know?

      splice( @img, -20 )= @img; $links = join(",", @img), "\n";
      thanks for your patience,

      cdherold

      splice( @array, -20 )= ();   #array, removes scalarsCan't modify splice in scalar assignment

      I don't think you tested that code. splice can't be assigned to. You instead want: splice @array, -20;

              - tye (but my friends call me "Tye")
        Arghle.... ahum. Yes. Got carried away by substr, clearly. Thx for pointing that one out.
      Never mind about the last post ... got it with splice(@img,-20);

      thanks very much for your and everyone else's help.

      cdherold

Re: Variable Removal From End of String
by Eureka_sg (Monk) on May 01, 2001 at 11:18 UTC

    Assuming that each variable is a string containing no commas, you could do this:

    @string = split(',',$string); $newstring = join(',',@string[0..$#string-20]);

    I'm assuming that $string contains all the variables concatenate together as in:

     $string = "$a,$b,$c,....."
Re: Variable Removal From End of String
by Chady (Priest) on May 01, 2001 at 11:19 UTC

    ($a, $b, $c...) is a list and not a string, if that's the case, then you can pop them out of the array that you will put them in, if not, then your string would be like,

    $string = "$a$b$c";
    then you would have to check the length of the last 20 to substr your string to the desired length..

    try to be more precise in your questions...

    Update: jeroenes has already answered that...


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Variable Removal From End of String
by diarmuid (Beadle) on May 01, 2001 at 15:49 UTC
    I'm not sure I understand what you mean about variables so heres' what I understand by your question.

    Say you want to remove the last 20 letter/spaces from a sentence

    $sentence="Error Problem in read_vhdl No designs were read DBR 011"; $sentence =~ s/[\w\s]{20}$//;
    should be what you need.

    Diarmuid