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 TouchToneTommy 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 question

Status
Not open for further replies.

StoneSoup

Programmer
Sep 12, 2008
5
US

So, I have a hash of array. The hash and arrays are initialized without a given length, so both are supposed to expand. I have isolated my problem to the latter 2 of the following three lines.

my $oldLength = @hash{$level};
$hash{$currentLevel}[$oldLength]=$var1;
$hash{$currentLevel}[$oldLength+1]=$var2;

In the case where I have to expand an array that has already been made before, everything fails, but if I constantly increase my $currentLevel (and therefore make new arrays) everything seems to work. Is there a small problem that I am missing here?

Thanks
 
use push() to add data to the end of an array, unshift() to add data to the beginning of an array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It gives an error saying that you cannot put in a slice of a hash. Well it did when I used push
 
well, this looks wrong:

my $oldLength = @hash{$level};

maybe that should be:

my $oldLength = @{$hash{$level}};

in which case:

push @{$hash{$level}},'foo';

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Double check how your using your vars - try running the following and see if you get what you expect
Code:
#!/usr/bin/perl

$hash{'a'}[0] = 'a0' ;
$hash{'a'}[1] = 'a1' ;
$hash{'a'}[2] = 'a2' ;
$hash{'a'}[3] = 'a3' ;
$hash{'b'}[0] = 'b0' ;
$hash{'b'}[1] = 'b1' ;

$level = 'a' ;
$oldLength = @hash{$level};
print "len $level = $oldLength\n";

$level = 'b' ;
$oldLength = @hash{$level};
print "len $level = $oldLength\n";

$level = 'a' ;
$oldLength = @{$hash{$level}};
print "len $level = $oldLength\n";

$level = 'b' ;
$oldLength = @{$hash{$level}};
print "len $level = $oldLength\n";
 
Nice examples Pinkey.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, I tried what you said and I got this error:
"Can't use an undefined value as an ARRAY reference
 
one of your variables is not defined. See Pinkeys code and you see it works.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
$level is defined in my code. I redid the length line as you recommended, but now I get no output at all. :(
 
post your code and let us know what $level is

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
before this there is a while loop and $level is initialized before that. $level is like a counter so it is incremented each time the while loop executes. Would there be a problem if $hash{$level} is undefined and I am trying to get the length of that undefined array?
 
probably

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

Part and Inventory Search

Sponsor

Back
Top