For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > iterate multidimensional array









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 iterate multidimensional array
alex.lorca@gmail.com

2005-11-02, 9:55 pm

hi

i have a multidimensional array of 5000x1000 but it has only one or two
elements in each row and the other are undef. Is there a way to quickly
find the index of the defined elements? or not iterate through the
undef elements?

thanks

usenet@DavidFilmer.com

2005-11-02, 9:55 pm

alex.lorca@gmail.com wrote:
> i have a multidimensional array of 5000x1000 but it has only one or two
> elements in each row and the other are undef.


That sounds dreadfully inefficient. Instead of an array of arrays
(which is what you are using), have you considered an array of hashes,
or a hash of hashes?

For example, suppose you had an array of arrays (call it @thing)
structured like this (where "-" means undef):

col0 col1 col2 col3 col4
row0 - - foo - bar
row1 bax - - - baz

So $thing[0][4] eq 'bar' and $thing[0][3] is undef. It works, but it's
inefficient and hard to "analyze" like you want.

The info could, however, be expressed as an array of hashes like this:

my @thing = (
{2 => 'foo', 4 => 'bar'}, #row 0
{0 => 'bax', 4 => 'baz'} #row 1
);

Now $thing[0]{4} eq 'bar' and $thing[0]{3} doesn't even exist (so you
only define the actual things that exist).

If you want to see what "columns" are defined for row 1, you would
simply:

print map{"$_\n"} sort keys %{$thing[1]};

Kewl.

--
http://DavidFilmer.com

Aless

2005-11-03, 7:55 am


usenet@DavidFilmer.com ha escrito:

> alex.lorca@gmail.com wrote:
>
> That sounds dreadfully inefficient. Instead of an array of arrays
> (which is what you are using), have you considered an array of hashes,
> or a hash of hashes?
>
> For example, suppose you had an array of arrays (call it @thing)
> structured like this (where "-" means undef):
>
> col0 col1 col2 col3 col4
> row0 - - foo - bar
> row1 bax - - - baz
>
> So $thing[0][4] eq 'bar' and $thing[0][3] is undef. It works, but it's
> inefficient and hard to "analyze" like you want.
>
> The info could, however, be expressed as an array of hashes like this:
>
> my @thing = (
> {2 => 'foo', 4 => 'bar'}, #row 0
> {0 => 'bax', 4 => 'baz'} #row 1
> );
>
> Now $thing[0]{4} eq 'bar' and $thing[0]{3} doesn't even exist (so you
> only define the actual things that exist).
>
> If you want to see what "columns" are defined for row 1, you would
> simply:
>
> print map{"$_\n"} sort keys %{$thing[1]};
>
> Kewl.
>
> --
> http://DavidFilmer.com


thanks for the answer!!
i will try with a hash of hashes since i need to have undefined rows.
Is this better from the point of view of storage? should i use empty
arrays instead?
I have a _big_ numbers of this multidimensional array

Michael

2005-11-03, 6:56 pm

Cool indeed. What is the map{"$_\n"} portion of that last print
statement?

PerlPanda

2005-11-03, 6:56 pm

I prefer to use hash of hash (technically hashrefs of hashrefs) .
However since you need undefined rows you might use the array instead.
I do not believe it should affect your performance anymore than having
to iterate over the undef values.

PerlPanda

2005-11-03, 6:56 pm

It maps $_ to $_\n, in this case the keys of %{$thing[1]} which is the
cols.

PerlPanda

2005-11-03, 6:56 pm

Also _big_ numbers is rather arbitary. I met a guy who said "I have a
_big_ number" when in fact he only had around a thousand or two. Its
not big until someone loses a finger from trying to count them.

usenet@DavidFilmer.com

2005-11-03, 6:56 pm

Michael wrote:
> Cool indeed. What is the map{"$_\n"} portion of that last print statement?


It just prints each item of the list with a line-ending (one item per
line).

Some folks use
join "\n", @array

but that doesn't put a line ending on the last item (map does).

'map' is VERY powerful and useful - it can do a LOT more than just put
line-endings on stuff.

perldoc -f map

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com