Kevin Pfeiffer wrote:
In article <[EMAIL PROTECTED]>, Sudarshan Raghavan wrote:
Ling F. Zhang wrote:[...]
say I have array:
@a=["I","LOVE","PERL"]
This will do what you want[...]
my $arr_to_str = "@a";
Wow!
I tried a couple more variations - and wonder why the last one doesn't evaluate a count of the slice...
#!/usr/bin/perl use warnings; use strict;
my @array = qw/apples oranges pears/;
my $string = "@array"; print "$string\n"; # prints "apples oranges pears"
What I should also have mentioned is that the interpolation is done using the $" scalar. It defaults to a single space. But, it can be changed. I guess the safest approach would be
my $arr_to_str = join (' ', @a);
perldoc -f join perldoc perlvar #For info on $"
$string = join '', @array; print "$string\n"; # prints "applesorangespears"
$string = "@array[0..1]"; print "$string\n"; # prints "apples oranges"
$string = @array[0..1]; print "$string\n"; # prints "oranges" (why not "2"?)
array vs list, an array in a scalar context evaluates to the number of elements in it. The same does not apply to a list, infact I don't think there is a list in scalar context.
my $string = ('one', 'two', 'three'); #this will result in $string containing 'three'
Reason: What applies here is the comma operator, the result of the expression is the result of the last sub-expression.
@array[0..1] evaluates to a two element list ("apples", "oranges"). When this is assigned to a scalar, the result will be "oranges" (the last sub-expression)
-K
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]