[Chicago-talk] Array Slices - of columns

Jay Strauss me at heyjay.com
Thu Dec 3 13:24:24 PST 2009


Thanks Steve.

Long time no hear from you.

Jay

On Thu, Dec 3, 2009 at 8:54 AM, Steven Lembark <lembark at wrkhors.com> wrote:

>
> > If I have a 10x10 array of arrays like:
> >
> > [40 8 79 8 73 71 77 35 12 67]
> > [4 79 10 90 45 17 30 29 65 86]
> > [56 86 5 34 23 79 17 83 24 85]
> > [58 85 46 53 15 31 34 4 14 15]
> > [68 15 97 21 81 95 71 82 26 90]
> > [59 70 11 82 39 25 36 84 88 13]
> > [66 6 85 1 84 17 27 58 60 94]
> > [34 10 38 90 32 0 56 14 23 70]
> > [71 23 5 97 4 71 27 20 11 87]
> > [5 8 23 30 97 16 10 93 16 67]
> >
> > Is there any notation I can use to get "column 1", i.e. $a[0][0],
> $a[1][0],
> > $a[2][0]... without looping over the whole structure
> >
> > I need to find the max value in each column
> >
> > I know how to do it in a loop, but I thought maybe there was some neato
> way
> > in a slice.
>
> Not really: "slices" are sequential within the
> array. Since you have a collection of separate
> arrays a slice does not apply.
>
> If you have to do this regularly, an alternative
> is storing the data in a single list and taking
> every Nth item:
>
>    @big_array[ 0, 10, 20, 30, ... ]
>
> will take the first item out of a 10x10 array,
>
>    @big_array [ 0 .. 9 ]
>
> will take the first row.
>
> This is more-or-less the way that languages with
> multi-dimensinal arrays handle the issue, but is
> a pain to manage yourself by hand if the language
> doesn't deal with stride issues for you.
>
> It also requires that you pre-size the array, which
> isn't always easy or effecient.
>
> If you regularly have to slice the data, it may
> be effective to store it in a hash:
>
>    $data{ $x, $y } = $value;
>
> This also helps in cases where you regularly have
> sparse data. You can "slice" the data by factoring
> out the interesting keys:
>
>    grep { /$x ; .*/x } keys %data;
>
> will grab a given x value,
>
>    grep { /.* $; $y/x } keys %data;
>
> will grab a given y value.
>
> That or you can buffer the keys onto arrays when
> they are first used and then just use:
>
>
>    my @col_valz = max @data{ @{ $col_keyz[$col] } };
>
> or
>
>    my @row_valz = max @data{ @{ $row_keyz[$row] } };
>
> For a moderately small matrix or somewhat sparse
> matrix that is frequently sliced this actually
> works pretty well -- at the expense of setting up
> the column and row key arrays once.
>
> Otherwise, as usual, map is your friend :-)
>
> --
> Steven Lembark                                            85-09 90th St.
> Workhorse Computing                                 Woodhaven, NY, 11421
> lembark at wrkhors.com                                      +1 888 359 3508
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20091203/28d452bc/attachment.html>


More information about the Chicago-talk mailing list