On Jul 13, 12:03 pm, [EMAIL PROTECTED] (Inventor) wrote: > Thanks for helping with my question the other day, now I have > another. In my class I have an array of hashes
In Perl, when we say "array of hashes" we are using it as shorthand for "array of references to hashes". 99% of the time everyone is happy using shorthand but once in a while someone forgets that it's a shorthand and takes the shorthand literally - this leads to mistakes. > just fine. I use the zeroth element to store individual variables and > all the other elements to store variables that change over time. For > example, i have the variable $num_games stuffed in $self->[0] > {'num_games'}, Again another shorthand. You almost certainly don't stuff the variable $num_games into $self->[0]{'num_games'}. You stuff a copy of the contents of $num_games into $self->[0]{'num_games'}. There's nothing wrong with using shorthand descriptions provided you don't forget what they are short for. It seems to me that your problems arise from using shorthand descriptions then basing your expectations on a literal interpretation of those descriptions. > and then I have $w in each of the remaining array > elements like this: $self[1]{'w'}...$self[1489]{'w'}. Having an array where element 0 is of one structure and all the others are of another sounds like the wrong implementation. > I think that's all working right, but now I would like to put an array > @teams into the zeroth element of $self and it isn't working. Again another shorthand. There's no such thing as "the zeroth element of $self" you must mean "the element zero of the array a reference to which is in $self" . But actually you don't even mean that. You mean "the 'teams' element of the hash a reference to which is in element zero of the array a reference to which is in $self". Now you can't "put an array" into an element of a hash. You can only put in a scalar or reference. So either you want to put a reference to @teams or put a reference to an anonymous array containing a copy of the contents of @teams > I tried: > > $self->[0]{'teams'} = @teams; This stores a copy of current the number of elements in @teams in $self->[0]{teams} > and > > @self->[0]{'teams'} = @teams; This is invalid syntax but due to a historic bug in the perl compiler (that can't now be fixed) the LHS above be interpreted as if you'd said $self[0]{'teams'}. This is an operation on the variable @self which is a completely separate variable unrelated to $self. But you were, close. You meant: # Store reference to @teams $self->[0]{'teams'} = [EMAIL PROTECTED]; or # Copy contents of @teams into array referenced by $self->[0]{teams} @{$self->[0]{'teams'}} = @teams; You can also use # Store reference to array containing a copy of contents of @teams $self->[0]{'teams'} = [EMAIL PROTECTED]; If $self->[0]{teams} is initially undefined the 2nd and 3rd above are equivalent. If $self->[0]{'teams'} already holds an arrayref then the 2nd replaces the contents of the existing array whereas the 3rd replaces the array. That said, without knowing what you are doing I'd guess there's about a ~95% chance you want the 1st. > but when I try to access the array with > > foreach $team ($self->[0]{'teams'}) { > print $team.' '; > > } There is no array there. You are accessing the scalar $self->[0] {teams}. If you expect $self->[0]{teams} to be a reference to an array then you can assess that array as @{$self->[0]{teams}} Note: you should always declare all variables as lexically scoped in the smallest applicable scope unless there is a reason to do otherwise. This means that you should almost never see foreach with an iterator variable without my. foreach my $team ( @{$self->[0]{teams}} ) { I suggest that for debugging you use Data::Dumper to printout the whole data structure that $self references. (Note usually I'd just say "$self" not "the whole complex data structure that $self references" but I'm trying to avoid shorthands). > > foreach $team (@self->[0]('teams')) { You can't just make shit up.... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/