list of numbers

A

Andrew Moffat

Hi,

I have lots of files with three columns of numbers e.g.

1 2 3
4.1 5 6
7 8 9
......etc

I want to extract the third column e.g.

3
6
9
.....etc

The problem is that I can't just use a colrm type function because the
numbers in the first 2 columns aren't always the same number of figures (as
above). Each number is seperated by a tab. Can anybody think of a way around
this please. TIA.
 
D

David K. Wall

I have lots of files with three columns of numbers [snip]
Each number is seperated by a tab. Can
anybody think of a way around this please.

perldoc -f split
 
S

Sandman

Andrew Moffat said:
Hi,

I have lots of files with three columns of numbers e.g.

1 2 3
4.1 5 6
7 8 9
.....etc

I want to extract the third column e.g.

3
6
9
....etc

The problem is that I can't just use a colrm type function because the
numbers in the first 2 columns aren't always the same number of figures (as
above). Each number is seperated by a tab. Can anybody think of a way around
this please. TIA.

#!/usr/bin/perl
use strict;
use warnings;

my $data = "1\t2\t3
4.1\t5\t6
7\t8\t9";

foreach (split /\n/, $data){
my @cols = split /\t/;
print $cols[2] . "\n";
}

__END__
3
6
9
 
T

Tore Aursand

I have lots of files with three columns of numbers e.g.

1 2 3
4.1 5 6
7 8 9
.....etc

I want to extract the third column e.g.

3
6
9
....etc

The problem is that I can't just use a colrm type function because the
numbers in the first 2 columns aren't always the same number of figures (as
above). Each number is seperated by a tab. Can anybody think of a way around
this please.

Hmmm. This is a tough one. How can I use Perl to split() something? I
know I've read about that somewhere, but where...? Was it in the Perl
documentation? Nah - that would've been too obvious, don't you think?
Yeah, I won't even bother to check...
 
G

gumby

Sandman said:
Andrew Moffat said:
Hi,

I have lots of files with three columns of numbers e.g.

1 2 3
4.1 5 6
7 8 9
.....etc

I want to extract the third column e.g.

3
6
9
....etc

The problem is that I can't just use a colrm type function because the
numbers in the first 2 columns aren't always the same number of figures (as
above). Each number is seperated by a tab. Can anybody think of a way around
this please. TIA.

#!/usr/bin/perl
use strict;
use warnings;

my $data = "1\t2\t3
4.1\t5\t6
7\t8\t9";

foreach (split /\n/, $data){
my @cols = split /\t/;
print $cols[2] . "\n";
}

__END__
3
6
9


Here is another of of attacking the problem. I realize this may get
out of hand if you have quite a few columns but its just a thought.
the test file contains the following data:

1 2 3
4.1 5 6
7 8 9

The Code:

unless(open FILE, test)
{
print "Failed to open the test file ($!).\n";
}

while(<FILE>)
{
my $line = $_;
$line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;
print "$1 and $2 and $3\n";
}

close FILE;

The Output:
1 and 2 and 3
4.1 and 5 and 6
7 and 8 and 9

As you can see $(number) is the column you want as you rip through
the file. You can push the column onto an array using something like
push(@array, $3) and that would push the contents of each line in
column three onto @array. There is probably a \(letter) that will do
decimal numbers i know \d does digits. Anyway hope this helps
-gumby
 
P

Paul Lalli

gumby said:
Sandman <[email protected]> wrote in message
Here is another of of attacking the problem. I realize this may get
out of hand if you have quite a few columns but its just a thought.

That's precisely why you should use split instead of RegExps in this
case. Someone wise once said "Use Regexps when you know what you want
to save, use split when you know what you want to throw away." You
don't (necessarily) know what you want to save - how many columns there
are. You do know what you want to throw away - all the space between
the fields, regardless of how many there are.
the test file contains the following data:

1 2 3
4.1 5 6
7 8 9

The Code:

unless(open FILE, test)
{
print "Failed to open the test file ($!).\n";
}

If the file fails to open, you print a message to STDOUT, and then keep
going with the program? This should be

open my $file, 'test' or die "Failed to open the test file ($!).";
while(<FILE>)
while ( said:
{
my $line = $_;
$line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;

why reassign $_ to $line if all you're doing is pattern matching it one
statement later?
print "$1 and $2 and $3\n";

Never, ever use $1, $2, $3 unless you know the pattern match succeeded.
You never check for that.

Another reason to not use regexps here: What if the numbers are in the
format such as -0.9? How about 5e4? Again, you know what you want to
throw away, you don't know what you want to keep.
}

close FILE;

The Output:
1 and 2 and 3
4.1 and 5 and 6
7 and 8 and 9

As you can see $(number) is the column you want as you rip through
the file. You can push the column onto an array using something like
push(@array, $3) and that would push the contents of each line in
column three onto @array. There is probably a \(letter) that will do
decimal numbers i know \d does digits.

No, there's not. There is a module on the CPAN that will allow you to
specify exactly what kinds of numbers you want, however.
Regexp::Common, I believe.
Anyway hope this helps
-gumby

Paul Lalli
 
T

Tore Aursand

Here is another of of attacking the problem. I realize this may get out
of hand if you have quite a few columns but its just a thought. the test
file contains the following data:

1 2 3
4.1 5 6
7 8 9

The Code:

unless(open FILE, test)
{
print "Failed to open the test file ($!).\n";
}

Why continue if there's no point in continuing? Why should you even _try_
to read from the file if you didn't succeed in opening it?
while(<FILE>)
{
my $line = $_;
$line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;
print "$1 and $2 and $3\n";
}

Blah. A hairy regular expression instead of the easy split() function?
And why assign $_ to $line, instead of directly? And what happens if the
regular expression doesn't match?

while ( <FILE> ) {
if ( ... ) {
# match
}
}
 
C

Charles DeRykus

Hi,

I have lots of files with three columns of numbers e.g.

1 2 3
4.1 5 6
7 8 9
.....etc

I want to extract the third column e.g.

3
6
9
....etc

The problem is that I can't just use a colrm type function because the
numbers in the first 2 columns aren't always the same number of figures (as
above). Each number is seperated by a tab. Can anybody think of a way around
this please. TIA.

Unix: perl -F\\t -lane 'print $F[2]' file1 file2...

W2K: perl -F\t -lane "print $F[2]" file1 ...

hth,
 
M

Michele Dondi

1 2 3
4.1 5 6
7 8 9
.....etc

I want to extract the third column e.g.
[snip]
Unix: perl -F\\t -lane 'print $F[2]' file1 file2...

W2K: perl -F\t -lane "print $F[2]" file1 ...

Going down the golfing route, I'd suggest

perl -lape'$_=pop@F'

(-a defaults split()ting on whitespace and "\t" *is* whitespace.)
Without the need of being so drastic, though, it is amazing to see
what kind of clumsy solutions have been posted to this simple
problem...


Michele
--
#!/usr/bin/perl -lp
BEGIN{*ARGV=do{open $_,q,<,,\$/;$_}}s z^z seek DATA,11,$[;($,
=ucfirst<DATA>)=~s x .*x q^~ZEX69l^^q,^2$;][@,xe.$, zex,s e1e
q 1~BEER XX1^q~4761rA67thb ~eex ,s aba m,P..,,substr$&,$.,age
__END__
 
M

Michele Dondi

Golf, eh?

perl -pe's/.*\t//'

D'Oh!


Michele (who is slightly ashamed because he's been thinkg of this very
same solution *in connection with sed* mentioned in Abigail's post,
and even does something similar with either "/" or "\." instead of
"\t" almost on a daily basis either with perl or sed...),
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top