Home > Archive > PERL Beginners > November 2005 > Perl Join strange behaviour
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Perl Join strange behaviour
|
|
| jcom2002@hotmail.com 2005-11-11, 3:56 am |
| Hi all,
Try to print a list:
1. perl -e 'print join "|" => qw(good days), "\n";'
2. perl -e 'print join("|", qw(good days)), "\n";'
3. perl -e '$foo = join "|" => qw(good days); print "$foo\n";'
Output:
1. good|day|
2. good|day
3. good|day
The first one gimme an extra trailing "|"!
I used to think these 3 lines should produce the same output.
perl v5.8.6
| |
| wisefamily@integrity.com 2005-11-12, 7:55 am |
| jcom2002@hotmail.com wrote:
> Hi all,
>
> Try to print a list:
>
> 1. perl -e 'print join "|" => qw(good days), "\n";'
I think your problem is in the ambiguity of the missing parentheses
around the join function. That line (theoretically) could be
interpreted as
print(join("|", qw(good days)), "\n");
the way you think it should be, but because the join function takes a
scalar followed by an array, perl interprets it like
print(join("|", qw(good days), "\n"));
and print is passed
"good|days|\n"
Also, why are you using the "=>" operator instead of the "," operator?
The "=>" operator is usually only used in hashes, and using in
functions is confusing.
> Output:
>
> 1. good|day|
> 2. good|day
> 3. good|day
I think you mean
1. good|days|
2. good|days
3. good|days
(with days instead of day).
Hope this helps,
David
| |
|
| Thanks David.
U're right, join as a function will just exhaust all arguments on the
right hand side.
And "day" should be "days", my typo.
|
|
|
|
|