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

Good afternoon folks,

I started programming with perl last week and perl is my first language so I'm a bit of a newbie. Basically I need help with a multidimensional array.

I have a 100k by 3 multidimensional array, contained in a CSV file. Each constituent array is a point in a 2D space with an intensity value taking up the third entry. But I need perl to calculate the norms of each constituent array and add those as a fourth column to this multidimensional array. So I would have a 100k by 4 multidimensional array.

Here's my code:
sub norm{ $p=$#data; #I call the 100k x 3 array @data for($i=0;$i<=$p; $i++){ # iterates down @data $norm=(($data[$i][0])**2+($data[$i][1])**2)**(1/2); #calculate +s the norm push(@data[i],$norm); #pushes the norm into the correct row of + @data, yet I get an error. } }

Specifically my syntax checker tells me that the "Type of arg 1 to push must be array (not array slice) near "$norm)"

Aid me wise monks!

Replies are listed 'Best First'.
Re: pushing multidimensional arrays
by ikegami (Patriarch) on Jul 05, 2010 at 20:17 UTC

    There's not really such thing as a 2d array in Perl. You have arrays of references to arrays, so $data[$i] is a reference to an array.

    If you'd normally use @array, the reference equivalent is @{ $aref }, as per References Quick Reference. Therefore, you want

    push @{ $data[$i] }, $norm;

    or simply

    $data[$i][3] = $norm;
Re: pushing multidimensional arrays
by moritz (Cardinal) on Jul 05, 2010 at 20:19 UTC
    In addition to what others already wrote, let me point you to perllol, which specifically discusses arrays of array references.
Re: pushing multidimensional arrays
by Anonymous Monk on Jul 05, 2010 at 20:12 UTC
Re: pushing multidimensional arrays
by roboticus (Chancellor) on Jul 06, 2010 at 11:21 UTC

    afalsename:

    You've already received guidance to solve your problem. But I'm bored, so...

    I first glued a bit more code to your example so I could try it out. (You'll get more (and frequently better) responses if you perform this step yourself. I frequently ignore requests where the author doesn't perform this step. But I'm bored this morning.)

    Next, I corrected the problem giving me this:

    my @data = ( [1 .. 3], [4 .. 6], [7 .. 9] ); norm(); print join(", ", @{$_}), "\n" for @data; sub norm{ $p=$#data; #I call the 100k x 3 array @data for($i=0;$i<=$p; $i++){ # iterates down @data $norm=(($data[$i][0])**2+($data[$i][1])**2)**(1/2); #calculate +s the norm $data[$i][3] = $norm; } }

    Next, I added strict and warnings, and made a couple changes to clean things up:

    Once all that was done, I noticed that $norm was used only once, so I removed it:

    sub norm{ my $ar = shift; for(@$ar){ # iterates down @data $$_[3]=(($$_[0])**2+($$_[1])**2)**(1/2); #calculates the norm } }

    Okay, now, I'm just about awake now, so I think I'll wrap this up and make myself some coffee and breakfast...

    ...roboticus

    Update: Added readmore tags.

Re: pushing multidimensional arrays
by punkish (Priest) on Jul 06, 2010 at 18:51 UTC
    This is the kind of problem for which PDL was created. I have only just started learning PDL, so my contrib below is going to be naive, but here is how I would do it
    # a [3 x 100K] piddle $a = sequence 3, 100_000 [ [ 0 1 2] [ 3 4 5] [ 6 7 8] .. ] # create an extra [1 x 100K] piddle to hold the norms $b = zeros 1, 100_1000 [ [0] [0] [0] .. ] # append $b to $a $c = $a->append($b) [ [ 0 1 2 0] [ 3 4 5 0] [ 6 7 8 0] .. ] # get the slices to the different cols $col1 = $c->slice('0,:') $col2 = $c->slice('1,:') $col4 = $c->slice('3,:') # calc the norms $col4 .= (($col1 ** 2) + ($col2 ** 2)) ** 0.5 [ [ 0 1 2 1] [ 3 4 5 5] [ 6 7 8 9.2195445] .. ] # or, do it all in one line $b = $a->append(zeros 1, 100_000) $norm = $b->slice('3,:') $norm .= (($b->slice('0,:') ** 2) + ($b->slice('1,:') ** 2)) ** 0.5

    I am sure PDL vets would improve the above many different ways, however, PDL is ideally suited for the kind of problem you are posing.

    --

    when small people start casting long shadows, it is time to go to bed
      The only thing I'd change in the above is to first initialise a block of zeroes (which these days is very quick) to the final size, then assign the starting information into it (using a slice, in standard PDL style), then do the calculation of norms:
      $out = zeroes 4,100_000; $out->slice('0:2')->inplace->sequence; $out->slice('3') .= (($out->slice('0') ** 2) + ($out->slice('1') ** 2) +) ** 0.5;
      The benefit of starting with the final size is there's only one big allocation. When loop-fusion arrives, the data will only go in and out of RAM once. Note the above slices don't need to specify the trailing ,:.