Matching and Regular Expressions

D

deadpickle

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

deadpickle

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

John W. Krahn

deadpickle said:
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
 
J

Jim Gibson

deadpickle said:
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)
 
J

Jim Gibson

deadpickle said:
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
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top