Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash of arrays - problem adding to the array

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
If'm testing to see if initially the hash's array value is empty. If it's not, I want to push a string value to the array.

I'm getting an error when I printing out the hash array values:
"can't use string ("2") as an ARRAY ref while "strict refs" in use".

This simple code works if I don't try to add to the array, so the problem is the bold statements.

What am I doing wrong? Thanks for any comments!!!

use warnings;
use strict;

my %ha_aid;

open (FILE,"fpf.temp") || ((warn "Can't open file: $!\n"), die);

while (<FILE>)
{
my @line = split;
if ($line[3] =~ /$ARGV[0]/)
{
if (@ha_aid{$line[2]})
{
print "not empty\n";
$ha_aid{$line[2]} = push (@{$ha_aid{$line[2]}}, $line [3]);
}
else
{
print "empty\n";
$ha_aid{$line[2]} = [$line[3]]; #anon array used?
}
}

}
close (FILE);

foreach my $x (keys %ha_aid)
{
print "Unique aid: $x \tFP: @{ $ha_aid{$x} } \n"
}

 
The line:
$ha_aid{$line[2]} = push (@{$ha_aid{$line[2]}}, $line [3]);
should have only been:
push (@{$ha_aid{$line[2]}}, $line [3]);

It now works fine !!!

Question though:
In this stmt. $ha_aid{$line[2]} = [$line[3]]; #anon array used?, I can't seem to use anything but the square anonymous array brackets - is there another way to assign an array from a scalar e.g. - = @($line[3])?
 
this:

Code:
 $ha_aid{$line[2]} = [$line[3]];  #anon array used?

Could probably just be:

Code:
 $ha_aid{$line[2]} = $line[3];  #anon array used?

But there might be a reason why the scalar is stored as an array even though its only an array with one element in it.

And watch out for sloppy code, the space between the array name ($line) and the index number ([3]) should not be there:

Code:
push (@{$ha_aid{$line[2]}}, $line [3]);

It is OK in that line of code but try something like that in other lines of code and it may not work and you will be stumped trying to figure out why:


Code:
@line = ('Kevin','Joe');
print "My name is $line [0]";




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kev -
Thanks for the reply ...
The reason the scaler value is stored in an array is because I plan to add more elements to this array as the file is read - thus the reason for the test to see whether the hash array is empty.

However, I just realized I don't even need to do this test since I could just push the scaler onto the array even though it's initally empty (I didn't relaize this before).
push (@{$ha_aid{$line[2]}}, $line [3]);

Oh, and those spaces between the indexes? ... copy-n-paste issues, not actually in the code.
 
Very good. Good luck with everything.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top