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

Hi Monks, I really hope you can help me out - I'm a newbie here. I have a dataset like this:

-2.65 -2.0865e-006 2.0831e-006
-2.6 -2.0532e-006 2.0569e-006
-2.55 -2.0215e-006 2.0262e-006

And I want to be able to access, and do calculations on, individual items within this dataset, using a 2(?) dimensional array. I want to be able to pick out, say, -2.0215e-006, and then I can use the corresponding values in each of the two remaining columns to do the calculation. But I can't seem to find a way to get at these individual items. Any help is very much appreciated. This is driving me crazy! Thanks!
Tedffo

Replies are listed 'Best First'.
Re: accessing array items
by FunkyMonk (Bishop) on Jul 29, 2008 at 10:48 UTC
    All in one line:
    my @data = map { [split] } <DATA>;

    Or, spelt out step by step...

    my @data; while ( <DATA> ) { #for each line of data my @row = split; #split on whitspace push @data, \@row; #push a reference to the row } print "$data[0][0]\n"; #-2.65 print "$data[1][0]\n"; #-2.6 print "$data[1][1]\n"; #-2.0532e-006 print "$data[1][2]\n"; #2.0569e-006 __DATA__ -2.65 -2.0865e-006 2.0831e-006 -2.6 -2.0532e-006 2.0569e-006 -2.55 -2.0215e-006 2.0262e-006

    You didn't mention where the data originally comes from, so we can't help you with that.

    See split, map and the Perl Data Structures Cookbook for more details.


    Unless I state otherwise, all my code runs with strict and warnings
Re: accessing array items
by moritz (Cardinal) on Jul 29, 2008 at 10:32 UTC
    You can get the individual items with split:
    chomp $line; my @items_in_line = split ' ', $line; # access the second item: print $items_in_line[1], "\n";
      thanks for the reply Moritz.
      I tried splitting, but it runs all items on column 1 into  $items_in_line[0], all items in column 2 into $items_in_line[1] etc. My problem is that I can't seem to get into, and be able to reference, individual items within, say, $items_in_line[0].
      Any further thoughts?
      Thanks v much again.
      Tedffo
        Here is a working example that demonstrates a bit more:
        use strict; use warnings; use Data::Dumper; my @matrix; while (<DATA>){ chomp; my @row = split; print "second item in row: $row[1]\n"; push @matrix, \@row; } print "And this is how the matrix look like:\n"; print Dumper \@matrix; print "Iterating over the matrix, getting the first column:\n"; for my $row (@matrix){ print $row->[0], "\n"; } __DATA__ -2.65 -2.0865e-006 2.0831e-006 -2.6 -2.0532e-006 2.0569e-006 -2.55 -2.0215e-006 2.0262e-006

        If that doesn't help you, please show us the code you're using, and what you want it to print out.

        See also perlreftut, perldata, perldsc.

Re: accessing array items
by davorg (Chancellor) on Jul 29, 2008 at 11:03 UTC
      OK, it's working now how I want it too. Now I can fight a bit more with the rest of the code... oh man.
      Thanks folks. Your help is much, much appreciated. Cheers!
Re: accessing array items
by apl (Monsignor) on Jul 29, 2008 at 10:44 UTC
    Is the information in a text file, a database, or hard-coded in your script?

    Are you asking how to use arrays in Perl?

    If you could show us the code you've written so far, we'd be happy to help you move along the spiritual path that is Perl...

      Yes, I guess this is a question on how to manipulate arrays in Perl. The data is in a text file. The code looks like this:
      foreach (<>) { $line=$_; if ($line =~ m(^\s{2}[-0])) #gets us to required lines in dataset { chomp $line; @items_in_line = split ' ', $line; } print $items_in_line[o]; }
      Prob is, I cant seem to get at the individual numbers on each line.
      thanks again!
        $items_in_line[3] would give you the fourth value on the line. (Indices start at 0, though you can change that if you need to.)

        Be careful to make certain there really is a fourth value before you try to manipulate it. ($#items_in_line is the number of elements in the array.)