Home > Archive > PERL Beginners > November 2005 > What kind of structure to choice
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What kind of structure to choice
|
|
| Gilles 2005-11-03, 7:55 am |
| Hi,
I would like to create a matrix 9x9 where the elements are list (array).
(like a cube)
These lists will decrease during processing and will normally contain one
element, so to know if a list is definitely treated I need a flag.
So 2 solutions seems to be good, but which one do you think will be easier
to use :
- an array of array of array with as flag a special value at the
end of the last array
- an array of array of hash with one key for the list of value and
one key for the flag
In the 2 solutions, I'm not sure to know how to access the lists I have to
modify !
Could you help me please
Thanks
Gilles
| |
| Jeff 'japhy' Pinyan 2005-11-03, 7:55 am |
| On Nov 3, Gilles said:
> These lists will decrease during processing and will normally contain one
> element, so to know if a list is definitely treated I need a flag.
Why? You can find the size of an array like so:
$size = @array;
In the case of an array reference, it's merely
$size = @{ $aref };
What are you doing that leads you to believe you'll need a special marker
of some sort to indicate that an array has been processed?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
| |
| Shawn Corey 2005-11-03, 6:56 pm |
| Gilles wrote:
> Hi,
>
> I would like to create a matrix 9x9 where the elements are list (array).
> (like a cube)
>
> These lists will decrease during processing and will normally contain one
> element, so to know if a list is definitely treated I need a flag.
>
> So 2 solutions seems to be good, but which one do you think will be easier
> to use :
>
> - an array of array of array with as flag a special value at the
> end of the last array
>
> - an array of array of hash with one key for the list of value and
> one key for the flag
>
>
>
> In the 2 solutions, I'm not sure to know how to access the lists I have to
> modify !
>
>
>
> Could you help me please
>
> Thanks
>
> Gilles
The best way to use a multi-dimensional array is to simply use it:
my @array = ();
# Adding an item to the $i, $j element
push @{ $array[$i][$j] }, $item;
# Retrieving an item
$item = pop @{ $array[$i][$j] };
# Accessing the $k item
$item = $array[$i][$j][$k];
# Iterating over a list
for my $item ( @{ $array[$i][$j] } ){
# Any change to $item will change
# the element in the list @{$array[$i][$j]}
}
# To see inside the matrix
use Data::Dumper;
print Dumper( \@array );
# For your other problem, I would simply keep a separate array.
my @Processed = ();
# for all $i, $j
unless( $Processed[$i][$j] ){
# Process @{$array[$i][$j]}
$Processed[$i][$j] = 1;
}
--
Just my 0.00000002 million dollars worth,
--- Shawn
"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
|
|
|
|
|