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

I've posted before on this subject but have failed to mention a couple of things. I'm trying to open a file that contains the following data:

block 2022, 2002 6, 78 1023, 1232 etc...

The data format can not change. I want to read the data into an array. This is what I have:

open (DAT, "data.dat") || die ("Error: Could not open the selected fil +e..."); @encoded_ECGdata = <DAT>;

This seems to work (I do not have the speech marks around the DAT within the array declaration I have sharp brackets, I just could not remember how to get it to appear within HTML). Is this right?

Next, I want to read the data in the array created from the file into two seperate arrays. All the data on the left-hand side into one array and all the data on the right-hand side into the other. I would like to keep the word 'block' at the same interval within each array but lose the comas. I would also like to manipulate the data later. Lastly how would I go about outputting the data in the two newly created arrays.

This thing has been driving me nuts - I've just started using Perl last week.

Sorry for the repost, guys.

Updated 2002-04-08 by mirod: added proper code tags

Replies are listed 'Best First'.
Re: Reading arrays
by Fletch (Bishop) on Apr 08, 2002 at 19:15 UTC
    open( DAT, "data.dat" ) or die "Can't open data.dat: $!\n"; my( @left, @right ); while( <DAT> ) { chomp; if( /^block/ ) { push @{$_}, "block" foreach \@left, \@right; next; } my( $left, $right ) = split( /,/ ); foreach( $left, $right ) { s/^\s+//; s/\s+$//; } push @left, $left; push @right, $right; } close( DAT );
Re: Reading arrays
by tadman (Prior) on Apr 08, 2002 at 19:43 UTC
    The five minute introduction goes something like this.
    use strict; # Warns about mistakes (important) my @data; # Data storage variable my $filename = "data.dat"; # Filename constant # Open a handle to the specified file, or die with a # warning that it could not be done. open (DAT, $filename) || die "Could not open $filename\n"; # Now read through the contents of the file while (<DAT>) { chomp; # Removes trailing linefeed from input if ($_ eq "block") { # Put "block" into the two columns push (@{$data[0]}, $_); push (@{$data[1]}, $_); } elsif (/^\s*(\d+),(\d+)/) # Look for " nn,nn" { # The first group of digits, expressed as "\d+", # is 'memorized' as $1, with the second group as # $2 (and so on). \s* means "0 or more spaces" # and is not memorized (ignored) push (@{$data[0]}, $1); push (@{$data[1]}, $2); } else { # $. is the current input line (perldoc perlvar) print "Invalid input line $.\n"; } } close (DAT);
    This might seem a bit confusing since it is using a multi-dimensional array, but it's not really a big deal. The @{} part is used to specify that you want to deal with the entire column array, and not a specific element. A specific element is referenced as $data[$x][$y], such as $data[0][0] which is the first element of the first column.

    Now to display this, it is quite simple. This will output something quite similar to your input file, though it is being read from the @data array.
    for (my $i = 0; $i < @{$data[0]}; $i++) { print "$data[0][$i],$data[1][$i]\n"; }
    The data structure is a little complicated because you want to have each column in a separate array. If instead you put each row into the array, it would be much simpler.
Re: Reading arrays
by Blanco (Initiate) on Apr 08, 2002 at 19:35 UTC
    This seems to work well. Would I be able to manipulate the data? I've output the arrays like this:

    print @right;

    It simply runs together. How would I add 2022 and 6 for example or 2002 and 78 or some other calculations?

    Cheers
      This is going to sound harsh, but go buy a book on Perl. You're asking extremely simplistic questions about array manipulations and data manipulations. You'll find that it's better to learn to fish than be given one.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.