sockmonkey
08-05-2002, 08:46 PM
Many thanks in advance for your help! First, I'll show you what I'm trying to do and then the code I'm using.
OBJECTIVE:
I'd like to query a text file with the following numbers organized as such:
10 35
20 75
45 50
50 98
36 45
Output:
Ave. column 1: 32.2
Ave. column 2: 60.6
THE CODE:
So what I'm trying to do first is to query the lines individually and just print the output to see if it's working.
#!/usr/bin/perl -w
use strict;
open DATA, 'data.txt' or die "Can't open data.txt: $!\n";
my @data;
# get the data
while ( <DATA> ) {
my @d = /(\d+)/g;
push @{$data[$_]}, $d[$_] for 0 .. $#d;
}
for ( @data ) {
# assign line number [x] to a variable
my $line = @$_[0];
# trying to separate the numbers on the line
my @numbers = split (/ /, $line);
# just print the first line's first number
my $test_var = $numbers[0];
print "$test_var\n";
}
The Output:
10
35
Printed both numbers on separate lines. I can't figure out what I'm doing wrong but it must have something to do with my use of 'split'. Good karma points for anyone that helps! TIA!
OBJECTIVE:
I'd like to query a text file with the following numbers organized as such:
10 35
20 75
45 50
50 98
36 45
Output:
Ave. column 1: 32.2
Ave. column 2: 60.6
THE CODE:
So what I'm trying to do first is to query the lines individually and just print the output to see if it's working.
#!/usr/bin/perl -w
use strict;
open DATA, 'data.txt' or die "Can't open data.txt: $!\n";
my @data;
# get the data
while ( <DATA> ) {
my @d = /(\d+)/g;
push @{$data[$_]}, $d[$_] for 0 .. $#d;
}
for ( @data ) {
# assign line number [x] to a variable
my $line = @$_[0];
# trying to separate the numbers on the line
my @numbers = split (/ /, $line);
# just print the first line's first number
my $test_var = $numbers[0];
print "$test_var\n";
}
The Output:
10
35
Printed both numbers on separate lines. I can't figure out what I'm doing wrong but it must have something to do with my use of 'split'. Good karma points for anyone that helps! TIA!