G'day RHC_ICT,

"However, I would like a faster processing method if at all feasible."

The following is based on these assumptions:

Here's my tips for faster processing:

Benchmark to compare the validity of any of those. Here's one I did last week to compare for and map: "Re^5: How Perl can push array into array and then how retrieve [Benchmark]".

Here's some sample code:

#!/usr/bin/env perl use strict; use warnings; my @array=(); $array[0][0] = "ape"; $array[0][1] = "bear"; $array[0][2] = "cat"; $array[1][0] = "dog"; $array[1][1] = "emu"; $array[1][2] = "fox"; $array[2][0] = "goat"; $array[2][1] = "horse"; $array[2][2] = "ibex"; for (qw{goat gout dog goatee ape}, "goat's milk") { my $found = search_col(\@array, 0, $_); print "'$_' ", (defined $found ? "found on row $found" : 'not found' ), "\n"; } sub search_col { my ($array, $col, $search) = @_; my $result; for my $row (0 .. $#$array) { next unless $array->[$row][$col] eq $search; $result = $row; last; } return $result; }

Output:

'goat' found on row 2 'gout' not found 'dog' found on row 1 'goatee' not found 'ape' found on row 0 'goat's milk' not found

And benchmark that solution with any other solutions that are presented.

Finally, bear in mind that speed optimisations on small datasets are almost always a waste of time. If your data really is a 3x3 matrix, I'm sure you could get the correct result by visual inspection, faster than it would take you to type in the command to get the computer to do it for you. In general, aim to run benchmarks on real data, not on tiny samples.

— Ken


In reply to Re: Index 2D Array by kcott
in thread Index 2D Array by RHC_ICT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.