Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Matching and Regular Expressions

Reply
Thread Tools

Matching and Regular Expressions

 
 
deadpickle
Guest
Posts: n/a
 
      11-20-2006
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.

 
Reply With Quote
 
 
 
 
deadpickle
Guest
Posts: n/a
 
      11-20-2006
Here is a quick code I came up with:

source
===================================
!2345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890
!


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.

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-20-2006
deadpickle wrote:
> Here is a quick code I came up with:
>
> source
> ===================================
> !2345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890
> !
>
>
> 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
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      11-20-2006
In article <(E-Mail Removed) .com>,
deadpickle <(E-Mail Removed)> 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)
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      11-21-2006
In article <(E-Mail Removed) .com>,
deadpickle <(E-Mail Removed)> 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,$ele v) =
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Tutor] matching a street address with regular expressions Shawn Milochik Python 15 10-12-2007 11:50 PM
Regular expressions - matching strings sconeek@gmail.com Java 2 04-04-2006 02:48 PM
Re: Matching of optional parts in regular expressions Markus Elfring Python 1 07-08-2004 04:03 PM
matching curly braces and regular expressions Dustin D. Perl 0 08-26-2003 11:18 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments