Matching and Regular Expressions

Discussion in 'Perl Misc' started by deadpickle, Nov 20, 2006.

  1. deadpickle

    deadpickle Guest

    I need to search a txt document for several strings and then report
    them in an array. The source document looks as follows:

    AR FLIPPIN (AWOS) KFLP FLP 36 17N 092 35W 219 X
    W 7 US
    AR FORT SMITH KFSM FSM 72344 35 20N 094 22W 140 X U
    A 3 US
    AR SLATINGTON MTN. KSRX SRX 35 17N 094 22W 195 X
    8 US
    AR HARRISON KHRO HRO 36 16N 093 09W 417 X T
    A 6 US
    AR HOT SPRINGS KHOT HOT 34 29N 093 06W 162 X T
    A 6 US
    AR JONESBORO KJBR JBR 35 50N 090 39W 79 X T
    A 6 US
    AR LITTLE ROCK KLIT LIT 34 44N 092 14W 79 X U
    A 0 US

    The string I want to search for is KFSM and KJBR and I want the
    following numerical values reported to an array. I started to try and
    make this but I dont understand regular expressions very well. Thanks
    for the help.
     
    deadpickle, Nov 20, 2006
    #1
    1. Advertising

  2. deadpickle

    deadpickle Guest

    Here is a quick code I came up with:

    source
    ===================================
    !23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    !


    NORTH DAKOTA 24-JUN-05
    CD STATION ICAO IATA SYNOP LAT LONG ELEV M N V
    U A C
    ND BISMARCK KBIS BIS 72764 46 46N 100 45W 505 X X U
    X A F 0 US
    ND DEVILS LAKE KDVL DVL 72757 48 07N 098 55W 443 X
    W 7 US
    ND DEVILS LAKE KP11 P11 72758 48 07N 098 55W 443 X
    W 7 US
    ND DICKINSON KDIK DIK 46 48N 102 48W 788 X U
    A 6 US
    ND FARGO KFAR FAR 72753 46 56N 096 49W 277 X U
    A 1 US


    Code
    ====================================
    use strict;
    use warnings;
    open (ID, "stations.txt") || die ("cannot open file!!");
    while (my $stations = <ID>) {
    my @station = split (/\r/,$stations);
    foreach (@station) {
    print $_;
    if (my %stid = $_ =~ m/(?:^\w+|\s+)(KBIS|KDVL)(\s+|\w+)/) {
    print %stid,"\n";
    if ( defined ($stid{KBIS})) {
    print $stid{KBIS};
    }
    }
    }
    }

    It does not return anything in the print after the hash is initialized.
     
    deadpickle, Nov 20, 2006
    #2
    1. Advertising

  3. deadpickle wrote:
    > Here is a quick code I came up with:
    >
    > source
    > ===================================
    > !23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    > !
    >
    >
    > NORTH DAKOTA 24-JUN-05
    > CD STATION ICAO IATA SYNOP LAT LONG ELEV M N V
    > U A C
    > ND BISMARCK KBIS BIS 72764 46 46N 100 45W 505 X X U
    > X A F 0 US
    > ND DEVILS LAKE KDVL DVL 72757 48 07N 098 55W 443 X
    > W 7 US
    > ND DEVILS LAKE KP11 P11 72758 48 07N 098 55W 443 X
    > W 7 US
    > ND DICKINSON KDIK DIK 46 48N 102 48W 788 X U
    > A 6 US
    > ND FARGO KFAR FAR 72753 46 56N 096 49W 277 X U
    > A 1 US
    >
    >
    > Code
    > ====================================
    > use strict;
    > use warnings;
    > open (ID, "stations.txt") || die ("cannot open file!!");
    > while (my $stations = <ID>) {
    > my @station = split (/\r/,$stations);
    > foreach (@station) {
    > print $_;
    > if (my %stid = $_ =~ m/(?:^\w+|\s+)(KBIS|KDVL)(\s+|\w+)/) {
    > print %stid,"\n";
    > if ( defined ($stid{KBIS})) {
    > print $stid{KBIS};
    > }
    > }
    > }
    > }
    >
    > It does not return anything in the print after the hash is initialized.


    Perhaps you want something like this:

    use strict;
    use warnings;

    open ID, '<', 'stations.txt' or die "cannot open 'stations.txt' $!";

    while ( my $stations = <ID> ) {
    next unless $stations =~ /\A.{20}(KBIS|KDVL)\s+(.+)/;
    print "$1: $2";
    }




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 20, 2006
    #3
  4. deadpickle

    Jim Gibson Guest

    In article <>,
    deadpickle <> wrote:

    > I need to search a txt document for several strings and then report
    > them in an array. The source document looks as follows:
    >
    > AR FLIPPIN (AWOS) KFLP FLP 36 17N 092 35W 219 X
    > W 7 US
    > AR FORT SMITH KFSM FSM 72344 35 20N 094 22W 140 X U
    > A 3 US
    > AR SLATINGTON MTN. KSRX SRX 35 17N 094 22W 195 X
    > 8 US
    > AR HARRISON KHRO HRO 36 16N 093 09W 417 X T
    > A 6 US
    > AR HOT SPRINGS KHOT HOT 34 29N 093 06W 162 X T
    > A 6 US
    > AR JONESBORO KJBR JBR 35 50N 090 39W 79 X T
    > A 6 US
    > AR LITTLE ROCK KLIT LIT 34 44N 092 14W 79 X U
    > A 0 US
    >
    > The string I want to search for is KFSM and KJBR and I want the
    > following numerical values reported to an array. I started to try and
    > make this but I dont understand regular expressions very well. Thanks
    > for the help.
    >


    In addition to Sherm's advice, for fixed-format lines such as these I
    would consider using the unpack function to extract the individual
    fields.

    perlddoc -f unpack
    perldoc -f pack (for the template rule elements)
     
    Jim Gibson, Nov 20, 2006
    #4
  5. deadpickle

    Jim Gibson Guest

    In article <>,
    deadpickle <> wrote:

    > Here is a quick code I came up with:


    [non-working program snipped]

    Here is a sample using unpack (I truncated data lines):

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    my($location,$date) = unpack "A20 A", <DATA>;
    <DATA>; # skip header row

    my %stid;
    while(my $line = <DATA>) {
    my($cd,$station,$icao,$iata,$synop,$lat,$long,$elev) =
    unpack 'A2 x A17 A4 x2 A3 x3 A5 x2 A6 x2 A7 A5', $line;
    $stid{$icao} = $iata;
    }

    if( exists $stid{KBIS} ) {
    print "IATA for KBIS is $stid{KBIS}\n";
    }

    __DATA__
    NORTH DAKOTA 24-JUN-05
    CD STATION ICAO IATA SYNOP LAT LONG ELEV
    ND BISMARCK KBIS BIS 72764 46 46N 100 45W 505
    ND DEVILS LAKE KDVL DVL 72757 48 07N 098 55W 443
    ND DEVILS LAKE KP11 P11 72758 48 07N 098 55W 443
    ND DICKINSON KDIK DIK 46 48N 102 48W 788
    ND FARGO KFAR FAR 72753 46 56N 096 49W 277
     
    Jim Gibson, Nov 21, 2006
    #5
    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. Dustin D.
    Replies:
    0
    Views:
    2,318
    Dustin D.
    Aug 27, 2003
  2. Jay Douglas
    Replies:
    0
    Views:
    618
    Jay Douglas
    Aug 15, 2003
  3. Replies:
    2
    Views:
    454
    Roedy Green
    Apr 4, 2006
  4. Noman Shapiro
    Replies:
    0
    Views:
    239
    Noman Shapiro
    Jul 17, 2013
  5. hymie!
    Replies:
    17
    Views:
    131
    Thomas 'PointedEars' Lahn
    May 22, 2014
Loading...

Share This Page