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

Hi, I'm a Perl noob so I don't know if it's a very stupid question. I have been assigned to write a program to do some stuff with excel files. I need to read the first column of IDs and ensure that all the corresponding etries in subsequent columns for each ID is a unique comination of characteristics. E.g. If I have 3 columns, "ID", "Product", "price", I need to check if all the IDs have a unique product and price. I can figure out a tedious C-ish solution for this but I believe Perl must have a prettier solution. I was trying out the module  Spreadsheet::Data which returns an arrayref of arrayrefs. Problem is I can't seem to be able to dereference the array without accessing each element separately. I mean, I know how to do  $$data[10][20];, but I can't use the array as a whole in loops or if statements. Can anyone please explain how to go about doing that, or even better, if you know of a better way to solve the problem, please let me know. It'd be a Huge help. Thanks, -m

Replies are listed 'Best First'.
Re: Dereferencing an arrayref of arrayrefs
by Corion (Patriarch) on Jan 28, 2010 at 10:00 UTC

    See the References Quick Reference. Basically, if you want to get at the array from a reference $ref you want @{ $ref }. So in a foreach-loop:

    foreach my $row (@{ $data }) { print "-- New row\n"; foreach my $col (@{ $row }) { print $col,"\n"; }; };
Re: Dereferencing an arrayref of arrayrefs
by BioLion (Curate) on Jan 28, 2010 at 10:03 UTC

    First - welcome to the Monastery!

    Second - for navigating / looping AoA (array of array) check out perldsc for a tutorial.

    Just a something something...
      There's even perllol, for "lists of lists" although it is really "Arrays of Arrays".
      Perl 6 - links to (nearly) everything that is Perl 6.

        Yeah - I forgot about that one, now if we can only access perldoc...

        Just a something something...
Re: Dereferencing an arrayref of arrayrefs
by molecules (Monk) on Jan 28, 2010 at 14:30 UTC

    My favorite way to deal with nested structures:

    use Data::Alias; ... alias my @rows = @{ $data }; for my $row (@rows) { alias my @col = @{ $row }; for my $col (@col) { print $col,"\n"; }; };
Re: Dereferencing an arrayref of arrayrefs
by manna45 (Novice) on Jan 29, 2010 at 02:55 UTC
    Thanks Monks. That was a lot of help and solved at least one part of the problem. However, I am still having some trouble with accessing only particular columns in the AoA. I want to do something like
    for ($i=0; $i<10; $i++) {print $data[1][$i]; }
    I know the syntax I've written here is not perfect. I've just kind of translated the logic into Perl to illustrate what I am getting at. So, can you tell me how do I go about doing this? Update: Ok never mind. I figured it out finally. Thanks for pointing out the relevant help docs. They helped!

      That would be better written:

      for my $index (0 .. 9) { print $data[1][$index]; }

      or if you want to print all the elements in the array reference you could:

      for my $index (0 .. $#{$data[1]}) { print $data[1][$index]; }

      or better still:

      for my $element (@{$data[1]}) { print $element; }

      and more succinctly as:

      print $_ for @{$data[1]};

      and if you would like a space between elements you can:

      print "@{$data[1]}";

      True laziness is hard work