Re: Issue with references and array slice with one member !
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Fri, 31 Oct 2008 08:38:07 -0400
On Fri, Oct 31, 2008 at 05:46, Amit Saxena <learn.tech123@xxxxxxxxx> wrote:
snip
I used "${$row}[0]", "${$row}[1]" etc one by one to get all the columns forsnip
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
${$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.
.
- References:
- Issue with references and array slice with one member !
- From: Amit Saxena
- Issue with references and array slice with one member !
- Prev by Date: RE: How to put a global variable in a package, accessible to users of that package?
- Next by Date: out of memory
- Previous by thread: Issue with references and array slice with one member !
- Next by thread: out of memory
- Index(es):
Relevant Pages
|