Problem with anonymous array in hash

B

bernd

Hello netties,

probably the solution is quite simple, but obviously I am blocked
currently concerning the following:

perl -e '@somearr=split(/ /,"11 12 13"); $hash{'array1'}->[0]=50;
$hash{'array1'}->[2]=100;printf("%d\n", scalar(@somearr));
printf("%d\n",
scalar(@$hash{'array1'}))'

Running this on the command line gives:

3
0

The first number is, as expected, the number of elements in the array
since evaluating an array in scalar context gives it's number of
elements. I would have expected "2" as the outcome for the second,
anonymous array stored in the hash, but obviously I am not referencing
to the array, or?

What am I doing wrong?

Cheers


Bernd
 
J

John W. Krahn

bernd said:
probably the solution is quite simple, but obviously I am blocked
currently concerning the following:

perl -e '@somearr=split(/ /,"11 12 13"); $hash{'array1'}->[0]=50;
$hash{'array1'}->[2]=100;printf("%d\n", scalar(@somearr));
printf("%d\n",
scalar(@$hash{'array1'}))'

Running this on the command line gives:

3
0

The first number is, as expected, the number of elements in the array
since evaluating an array in scalar context gives it's number of
elements. I would have expected "2" as the outcome for the second,
anonymous array stored in the hash, but obviously I am not referencing
to the array, or?

What am I doing wrong?

If you had enabled warnings perl would have given you a hint:

$ perl -we'
@somearr = ( 11, 12, 13 );
$hash{ array1 }[ 0 ] = 50;
$hash{ array1 }[ 2 ] = 100;
printf "%d\n", scalar @somearr;
printf "%d\n", scalar @$hash{ array1 };
'
3
Use of uninitialized value in printf at -e line 6.
0

The problem is that you are dereferencing $hash when you should be
dereferencing $hash{ array1 } like so:

printf "%d\n", scalar @{ $hash{ array1 } };


perldoc perlref



John
 
G

Gunnar Hjalmarsson

bernd said:
perl -e '@somearr=split(/ /,"11 12 13"); $hash{'array1'}->[0]=50;
$hash{'array1'}->[2]=100;printf("%d\n", scalar(@somearr));
printf("%d\n",
scalar(@$hash{'array1'}))'

Running this on the command line gives:

3
0

The first number is, as expected, the number of elements in the array
since evaluating an array in scalar context gives it's number of
elements. I would have expected "2" as the outcome for the second,
anonymous array stored in the hash, but obviously I am not referencing
to the array, or?

What am I doing wrong?

You are breaking The Golden Rule for debugging Perl code:

perldoc -q debug

Don't post buggy code here without first letting Perl help you fix it by
enabling strictures and warnings.
 
B

bernd

Thanks John, sometimes I am too concise with my code and omit things
which should not be omitted, e.g. braces ;-)
bernd said:
probably the solution is quite simple, but obviously I am blocked
currently concerning the following:

perl -e '@somearr=split(/ /,"11 12 13"); $hash{'array1'}->[0]=50;
$hash{'array1'}->[2]=100;printf("%d\n", scalar(@somearr));
printf("%d\n",
scalar(@$hash{'array1'}))'

Running this on the command line gives:

3
0

The first number is, as expected, the number of elements in the array
since evaluating an array in scalar context gives it's number of
elements. I would have expected "2" as the outcome for the second,
anonymous array stored in the hash, but obviously I am not referencing
to the array, or?

What am I doing wrong?

If you had enabled warnings perl would have given you a hint:

$ perl -we'
@somearr = ( 11, 12, 13 );
$hash{ array1 }[ 0 ] = 50;
$hash{ array1 }[ 2 ] = 100;
printf "%d\n", scalar @somearr;
printf "%d\n", scalar @$hash{ array1 };
'
3
Use of uninitialized value in printf at -e line 6.
0

The problem is that you are dereferencing $hash when you should be
dereferencing $hash{ array1 } like so:

printf "%d\n", scalar @{ $hash{ array1 } };


perldoc perlref



John
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top