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

Hi Monks, I have created an array similar to:
if (rule) {push @array,"1";} if (rule) {push @array,"2";} if (rule) {push @array,"3";}
When i print array each element is separated by whitespace. Is it possible to eliminate the white space characters stored in @array? Thanks

Replies are listed 'Best First'.
Re: remove whitespace from an array
by tlm (Prior) on Aug 03, 2005 at 11:52 UTC

    Probably all you need to do is print the array like this:

    print @array, "\n";
    and not like this
    print "@array\n";

    the lowliest monk

Re: remove whitespace from an array
by gellyfish (Monsignor) on Aug 03, 2005 at 12:05 UTC

    I think you are looking at two different things here, firstly if you print @array as:

    print "@array";
    you will get the elements of @array separated by the value of $" (aka $LIST_SEPARATOR if using the English module). so you can do:
    local $" = ''; print "@array";
    will not output the spaces - of course in this simple case you can simply omit the double quotes and the items will not be separated, I am assuming you are interpolating into a larger quoted string.

    If you want to remove any array elements that comprise only whitespace you can use grep :

    @array = grep !/^\s+$/, @array;
    You could also use map to strip leading or trailing whitespace from the elements:
    @code = map { s/^\s+//; s/\s+$//; $_ } @array;
    and so forth.

    /J\

Re: remove whitespace from an array
by blazar (Canon) on Aug 03, 2005 at 12:06 UTC
    There is no white space "stored" in @array. You're surely interpolating like thus: "@array". In which case, just... don't! Or else read up about $" in perldoc perlvar.
Re: remove whitespace from an array
by bangers (Pilgrim) on Aug 03, 2005 at 12:07 UTC
    From what you’ve written I doubt that the white space is in the array. It is probably being added when you print the contents. What happens if you print it out with the following
    print ‘[‘ . join(‘][‘, @array) .”]\n”;
      Huh?!? At first sight those quotes didn't look quite right to me, so I tried to download code and indeed this is what I got when opening with my editor:
      print ~^Q[~^Q . join(~^Q][~^Q, @array) .~^T]\n~^T;
      I suppose they're HTML entities. The actual code you wanted to show is probably:
      print '[' . join('][', @array) ."]\n";
        good catch! I'm an appalling speller and have gotten into the habit of typing posts into word first to ensure they are coherent. Stupidly I also typed my code in there and picked up Word's "smart" quotes.
      the use of $" appears to have done the job, thanks for your help. $" is yet another new feature to me Thanks
        And where is $" mentioned it the node you're replying to? It is mentioned in other replies, e.g. gellyfish's or my own. Not that I'm upset because I wanted to be thanked personally, only I think one should pay attention to attributions otherwise some confusion may arise...