Field matching

  • Thread starter Geezer From The Freezer
  • Start date
G

Geezer From The Freezer

Whats the best way at using field manipulation in perl (much like
awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

Assume I don't know how many fields are in each line of the input file too
 
G

Gunnar Hjalmarsson

Geezer said:
Whats the best way at using field manipulation in perl (much like
awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

Assume I don't know how many fields are in each line of the input file too

Which ways are you considering?
 
G

Geezer From The Freezer

Gunnar said:
Which ways are you considering?

I was considering sticking the whole file in an array and using a foreach $line
(@array)
then splitting the $line and then trying to compare the fields, but it seems
sloppy.
Just wondered if there was a simpler approach.
 
G

Geezer From The Freezer

Jim said:
Geezer From The Freezer said:
Whats the best way at using field manipulation in perl (much like
awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

Assume I don't know how many fields are in each line of the input file too

Use the split function to break up a line into tokens separated by
white-space (the default) or any other separator, which you can define
with a regular expression (see 'perldoc -f split' for more info). For
example:

while(<DATA>) {
my @f = split;
print if @f[1] == 3 && $f[2] == 9;
}

Jim,

Ok I'll give that a whirl!! Thanks!
 
J

Jeff Stampes

Jim said:
Whats the best way at using field manipulation in perl (much like
awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

Assume I don't know how many fields are in each line of the input file too


Use the split function to break up a line into tokens separated by
white-space (the default) or any other separator, which you can define
with a regular expression (see 'perldoc -f split' for more info). For
example:

while(<DATA>) {
my @f = split;
print if @f[1] == 3 && $f[2] == 9;
}

Make it easier and learn about perl's command line switches. If you're
used to awk (which I'm not), you can apparently do awk-like things:

perl -lane 'print if $F[1] == 3 && $F[2] == 9;' MyDataFile

tmp > perl -lane 'print if $F[1] == 3 && $F[2] == 9;' MyDataFile
Field1 3 9
tmp > cat MyDataFile
Field1 Field2 Field3
Quazi Plitch Plisk
Field1 3 9
Foo Bar Field3
Field1 Field2 Field3

My personal mnemonic for remembering -lane as the magic set of command
line switches: "perl can do what awk can...it's not lame, it's lane".

See 'perldoc perlrun' for more information.

~Jeff
 
W

William James

Geezer said:
Whats the best way at using field manipulation in perl (much like
awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

Assume I don't know how many fields are in each line of the input file too

The Awk code can be shorter:
awk '$2==3 && $3==9' infile

I think the command-line switches in Perl are almost the same as in
Ruby:

-F set field-separator
-a automatically split line into array $F
-n automatically read each line of the file
-p automatically read and print each line of the file
-l chomp each line read

This is equivalent to the Awk code; adapt it to Perl. You can
probably make it shorter:

ruby -nae 'print if $F[1].to_i==3 && $F[2].to_i==9' infile
 
G

Gunnar Hjalmarsson

Geezer said:
I was considering sticking the whole file in an array and using a
foreach $line (@array) then splitting the $line and then trying to
compare the fields, but it seems sloppy.

I see. As long as you only need to _get_ certain field values, Jim's
method is memory saving. If you also need to _set_ values, the Tie::File
module may be what you want.

use Tie::File;
tie my @rows, 'Tie::File', @file or die "Couldn't bind @file: $!";
for (@rows) {
my @f = split;
if ( $f[1] == 3 and $f[2] == 9 ) {
$f[3] = $f[1] + $f[2];
$_ = "@f";
}
}
untie @rows or die $!;
 
G

Geezer From The Freezer

William said:
The Awk code can be shorter:
awk '$2==3 && $3==9' infile

Yep I know, just using the 'print' part to show it will print (for non awk
people).
Gotta admit, I love awk!
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top