On Fri, Oct 31, 2008 at 05:46, Amit Saxena <[EMAIL PROTECTED]> wrote:
snip
> I used "${$row}[0]", "${$row}[1]" etc one by one to get all the columns for
> a particular code. On one of the production code which I am working, they
> have used "@{$row}[0]" , "@{$row}[0]" instead of earlier. The results for
> both of them were same.
>
> When I did a through debugging on this, I found that "@{$row}[0]" or
> "@{$row}[1]" etc is taken as array slice by perl with only one member so it
> returns the value of the column in scalar and not in list context. With
> "${$row}[0]" or "${$row}[1]" etc, the column value is returned in scaler
> context.
>
> I want to know whether it's appropriate to use "@{$row}[0]" , "@{$row}[1]"
> instead of "${$row}[0]", "${$row}[1]" though both of them gives same result
snip
${$row}[0] is better, but $row->[0] is best.
If you need ammunition to convince others then show them this code and
then run it on a fairly modern Perl interpreter. Perl will throw a
warning like "Scalar value @a[0] better written as $a[0] at z.pl line
8." It is interesting a warning isn't thrown for references. I shall
have to research that.
#!/usr/bin/perl
use strict;
use warnings;
my @a = qw/a b c/;
print @a[0], "\n";
All of that said, @{$row}[0] has its uses:
@{$row}[0] = qw(a b c);
produces difference results from
$row->[0] = qw(a b c);
But it is probably better written as
$row->[0] = (qw(a b c))[0];
to avoid confusing people.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/