$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}

J

jidanni

Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?
 
S

sln

Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?

Not at all.

$# @{ $h{$_} }

$h{$_} if correct is a sclar reference to an array.
@{} dereferences the context, assuming a ref to an array.
$# now knows where to look to get the size.

-sln
 
S

sln

Not at all.

$# @{ $h{$_} }

$h{$_} if correct is a sclar reference to an array.
@{} dereferences the context, assuming a ref to an array.
$# now knows where to look to get the size.

-sln

no less complicated than this:

my $self = {};
$self->{'vstr'} = [\(('hi ')x10)];
print "$$_ " for @{$self->{'vstr'}};
 
J

Jim Gibson

Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?

Yes. None of those will tell him how many elements are in the array.
Only the first is valid Perl, and gives the "highest index", which is
normally one less than the number of elements in the array.

The second gives me "Can't use string ("1") as an ARRAY ref while
"strict refs" in use at jidanni.pl line 12.", because an array
evaluated in scalar context returns the number of elements in the
array, and my test array had one element. (However, I just tried "perl
-Mstrict -e 'print $#{'1'};'", and that doesn't give me an error, so
maybe there is some other reason for the error.)

The third won't even compile:

"$# is no longer supported at jidanni.pl line 13.
Array found where operator expected at jidanni.pl line 13, at end of
line
(Missing operator before ?)
Can't use an undefined value as a symbol reference at jidanni.pl line
13."

Test program:

#!/usr/local/bin/perl
use strict;
use warnings;

my %h;
$_ = 'A';
$h{$_} = [];

push( @{$h{$_}}, 44 );

print $#{$h{$_}};
print $#{@{$h{$_}}}; # Can't use string ("1") as an ARRAY ref ...
print $#@{$h{$_}}; # no longer supported ...


There is no need to guess. The rule is that if @{X} is an array, then
$#{X} is the highest index of the array. 'X' can be a bare-word array
name or an array reference. If the former, then the braces {} are
optional. If X is a simple variable, e.g. $x, then the braces are also
optional.
 
A

A. Sinan Unur

Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?

First, a little whitespace will tell you which one of those makes sense.

But more importantly, why does the said user not use the array in scalar
context to get the number of elements in the array?

#!/usr/bin/perl

use strict;
use warnings;

my %h;

push @{ $h{test} }, 44;

print scalar @{ $h{test} }, "\n";

__END__


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
A

Alan Curry

Jim Gibson said:
array, and my test array had one element. (However, I just tried "perl
-Mstrict -e 'print $#{'1'};'", and that doesn't give me an error, so
maybe there is some other reason for the error.)

Because the 1 is not in quotes by the time perl gets it. Your -e program is
assembled by the shell from the 2 separate ''-delimited strings and the
unquoted 1 that's in between them. The code as perl sees it:

print $#{1}

is related to the array named "@1" which, since its name starts with a digit,
is exempt from the effects of your -Mstrict.

$ perl -Mstrict -e 'print $#{'1'}'
-1
$ perl -Mstrict -e 'print $#{"1"}'
Can't use string ("1") as an ARRAY ref while "strict refs" in use at -e line 1.
 
I

Ilya Zakharevich

Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?

It is very hard to understand what are you talking about; too many
wrong substatements...
perl -wle "$h{a}=[1..8]; print $#{$h{a}}"
7

Puzzled,
Ilya
 
R

Randal L. Schwartz

sln> Not at all.

sln> $# @{ $h{$_} }

sln> $h{$_} if correct is a sclar reference to an array.
sln> @{} dereferences the context, assuming a ref to an array.
sln> $# now knows where to look to get the size.

Wrong. You want $#{$h{$_}}.

$#@{...} wouldn't be write because that would mean $#@foo as the
non-reference equivalent, which is wrong. You want $#foo,
making that a reference is $#{...}.

The first rule we teach in the Alpaca class is to write whatever
you want like you never heard of references before, then replace
the name of the item with an expression contained in braces, and
that will be the dereferencing form.

print "Just another Perl hacker,"; # the original
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top