iKnowNothing has asked for the wisdom of the Perl Monks concerning the following question:
This produces:%Hash = ( ArrayInHash => [1,2,3,4], ); print "Array in hash: @$Hash{ArrayInHash}"; @Array = @$Hash{ArrayInHash}; print "\nArray: @Array"; $ArrayRef = $Hash{ArrayInHash}; @Array2 = @$ArrayRef; print "\nArray from ordinary scalar reference: @Array2"; print "\n\nTrying a foreach loop with the hash"; foreach(@$Hash{ArrayInHash}) { print "\n$_"; } print "\n\nTrying a foreach loop with the scalar array reference"; foreach(@$ArrayRef) { print "\n$_"; }
Why didn't the first two print statement or foreach loop act on the array? There's got to be a better way to access the array besides setting the reference to an ordinary scalar first. ThanksArray in hash: Array: Array from ordinary scalar reference: 1 2 3 4 Trying a foreach loop with the hash Trying a foreach loop with the scalar array reference 1 2 3 4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use hash of arrays
by saskaqueer (Friar) on Feb 15, 2005 at 21:24 UTC | |
by iKnowNothing (Scribe) on Feb 16, 2005 at 01:32 UTC | |
|
Re: How to use hash of arrays
by Animator (Hermit) on Feb 15, 2005 at 21:17 UTC | |
|
Re: How to use hash of arrays
by sh1tn (Priest) on Feb 15, 2005 at 21:18 UTC | |
|
Re: How to use hash of arrays
by tphyahoo (Vicar) on Feb 16, 2005 at 14:01 UTC | |
|
Re: How to use hash of arrays
by tphyahoo (Vicar) on Feb 16, 2005 at 09:09 UTC |