Field matching

Discussion in 'Perl Misc' started by Geezer From The Freezer, Aug 25, 2005.

  1. 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
     
    Geezer From The Freezer, Aug 25, 2005
    #1
    1. Advertising

  2. Geezer From The Freezer wrote:
    > 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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 25, 2005
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    >
    > Geezer From The Freezer wrote:
    > > 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?
    >


    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.
     
    Geezer From The Freezer, Aug 25, 2005
    #3
  4. Jim Gibson wrote:
    >
    > In article <>, Geezer From The Freezer
    > <> wrote:
    >
    > > 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!
     
    Geezer From The Freezer, Aug 25, 2005
    #4
  5. Geezer From The Freezer

    Jeff Stampes Guest

    Jim Gibson wrote:
    > In article <>, Geezer From The Freezer
    > <> wrote:
    >
    >
    >>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
     
    Jeff Stampes, Aug 25, 2005
    #5
  6. Geezer From The Freezer wrote:
    > 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
     
    William James, Aug 25, 2005
    #6
  7. Geezer From The Freezer wrote:
    > Gunnar Hjalmarsson wrote:
    >> Geezer From The Freezer wrote:
    >>> Whats the best way at using field manipulation in perl (much like
    >>> awk where you can use: awk '$2 == 3 && $3 == 9{print}' )

    >>
    >> 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.


    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 $!;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 25, 2005
    #7
  8. William James wrote:
    >
    > Geezer From The Freezer wrote:
    > > 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


    Yep I know, just using the 'print' part to show it will print (for non awk
    people).
    Gotta admit, I love awk!
     
    Geezer From The Freezer, Aug 26, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mark
    Replies:
    0
    Views:
    363
  2. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    260
    Marc Bissonnette
    Jan 13, 2004
  3. Bobby Chamness
    Replies:
    2
    Views:
    261
    Xicheng Jia
    May 3, 2007
  4. Sound
    Replies:
    2
    Views:
    497
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    491
Loading...

Share This Page