Wolf Blaum wrote:
> 
> For Quality purpouses, Rob Dixon 's mail on Tuesday 27 January 2004 00:30 may 
> have been monitored or recorded as:
> 
> > The right conclusion for the wrong reasons Wolf! The spaces are the result
> > of interpolating the array into a string, and the presence of a newline on
> > each array element is immaterial:
> >
> >   my @arr = ('A', 'B', 'C', 'D');
> >   print "@arr\n";
> >   print @arr, "\n";
> >
> > **OUTPUT
> >
> >   A B C D
> >   ABCD
> 
> well, maybe Im totally wrong here, but getting these results :
> 
> $last printed out
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client

Yes. These are the values of each string before they are pushed onto the array
(In the order they are pushed but before they're sorted).

> @temparray printed out
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client
> TMR2,mpd_gw,50,w32-ix86,client

Didn't you mean to put spaces before the last three records? This will be the
result of

  print @temparray;

which is the same as

  print $temparray[0], $temparray[1], $temparray[2], $temparray[3];

not

  print "@temparray";

which is the same as

  print join ' ', @temparray;

> from this code:
> 
> Code:
> if (something){
> $last = "$tmrname,$gateway_hash{$gateway},$version,$interp,$type\n";
> #print "$last";
> push(@temparray, $last);
> } 
> 
> @temparray = sort @temparray;
> print "@temparray";
>
> Anthony asked, where the spaces came from, expecting the output of $last and
> @temparray to be the same.
>
> Of course, you are right about the interpolation used before the print : i 
> just meant to point out that the newlines AND the spaces in his print 
> "@temparray" are a result of the quotes used with print, and his attachment 
> of \n to $last before the push @temparray,$last -  without making it to 
> complicated.

Thew newlines in the output are there because they were in the original
strings. The spaces after the newlines are added because that's how arrays
are interpolated in double-quote context, but they're nothing to do with the
original newline! Whatever the last character in each element is, the interpolation
"@temparray" puts a space between the elements, as I showed with my simpler example
with just single-letter strings.

> (Tim also hinted to $").

Yes. Strictly "@temparray" is the same as

  join $", @temparray

but $" is one of the built-in variables that I think it's best to forget about,
as if you want something other than the default you can just write a 'join'
with the separator you want and avoid obscure code.

> Or did I totaly miss something.

Maybe. I'm not sure where you're misunderstanding what I wrote. Anyway, I hope this
helps.

Rob


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to