Olaf has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to extract or slice an individual row from a multidimensional arry. Below is genericized code, can you complete?
my @RowOne = ("a","b","c"); my @RowTwo = ("d","e"); my @RowThree = ("f","g","h","i"); my @TwoDimArray = (\@RowOne,\@RowTwo,\@RowThree); my $ChosenRow = 1; # #Now I need one of the following: #The Chosen Array my @ChosenArray = ??? #or #The number of elements in the Chosen Array my $NumberOfElements = ???
It seems simple enough, but my attempts have crashed and burned. And I haven't found the answer just floating about in cyberspace. Is there a one line way to do this, or do I have to create it in a loop?
thanks - olaf

Do you believe in miracles? Yes!

Replies are listed 'Best First'.
Re: Extracting a row(1D array) from a multidimensional array
by Corion (Patriarch) on Oct 03, 2007 at 21:16 UTC

    "crashed and burned" is not much of a hint, or at least a Perl error message that is unknown to me. At least you provided some rudimentary source, but we are also interested in your attempts, and how they failed for you. Perl is often quite specific in its error messages and the diagnostics pragma can often even provide verbose information on how to fix the error condition.

    You seem to be having trouble with references, so tye's References Quick Reference feels appropriate. If you have problems with that, consider the Perl Data Structures Cookbook or the Tutorials section on References, which also lists intro to references.

Re: Extracting a row(1D array) from a multidimensional array
by FunkyMonk (Bishop) on Oct 03, 2007 at 21:16 UTC
    If I've understood want you want, $TwoDimArray[$ChosenRow] will get you an arrayref of the chosen row. To get an array from the reference, dereference it using @{ ... }. ie
    my @ChosenArray = @{ $TwoDimArray[$ChosenRow] };

    To find the number of elements, assign @{ $TwoDimArray[$ChosenRow] } in scalar context:

    my $elements = @{ $TwoDimArray[$ChosenRow] };

      Thanks Funky (and the rest). That did it.
Re: Extracting a row(1D array) from a multidimensional array
by throop (Chaplain) on Oct 04, 2007 at 03:22 UTC
    I suspect you are also confusing rows and columns. At least, the problem seems trivial if you're putting rows in and trying to get rows out again. It's more interesting if you're putting in rows and trying to get column slices out. Following graff's notation:
    my @RowZero = ("a","b","c"); my @RowOne = ("d","e"); my @RowTwo = ("f","g","h","i"); my @TwoDimArray = (\@RowZero,\@RowOne,\@RowTwo); sub chooseColumn{ my($col, $Array2D)=@_; map {my $val =@{$_}[$col] ; $val = '' unless defined $vval; $val} @$Array2D} print join(' ', chooseColumn(1, \@TwoDimArray));
    should yield
    b e g
    (not tested)

    throop

Re: Extracting a row(1D array) from a multidimensional array
by graff (Chancellor) on Oct 04, 2007 at 00:56 UTC
    I don't know whether this helps, but your array names seem to show an "off by one" issue in your way of thinking. Change the array names to encourage correct thoughts...
    my @RowZero = ("a","b","c"); my @RowOne = ("d","e"); my @RowTwo = ("f","g","h","i"); my @TwoDimArray = (\@RowZero,\@RowOne,\@RowTwo); my $ChosenRow = 1; my @chosen_array = @{$TwoDimArray[$ChosenRow]}; my $num_chosen_items = @chosen_array; print "I chose row $ChosenRow (counting from 0),\n". " containing $num_chosen_items things: @chosen_array\n";
    Works for me, as far as it goes.