Using a variable in a matching string

D

deadpickle

I want the data to be saved to a text file in this format:
KGRI
KLNK
KHSI

And then reads into the program and then run through the match string.
I cant seem to get it to work correctly. Any help?


use strict;
use warnings;
$\ = "\n";
open ID, '<', 'stations.txt' or die "cannot open 'stations.txt' $!";
open TBL, 'stidtbl.txt' or die "cannot open 'stidtbl.txt' $!";
while ( my @tbl = <TBL> ) { #reads in a table of station IDs
my $idtbl = join ("|", @tbl);
chop ($idtbl);
print $idtbl;
while ( my $stations = <ID> ) { #finds the lat and long of a searched
station
next unless $stations =~ /\A.{20}($idtbl)\s+(.+)/;
my @id = split (" ",$2);
if (length($id[1]) == 5) {
chop ($id[3]);
chop ($id[5]);
my $p = ".";
my $lat = $id[2] . $p . $id[3];
my $long = $id[4] . $p . $id[5];
my @stid = ( $id[0], $lat, $long);
print "@stid";
}
else {
chop ($id[2]);
chop ($id[4]);
my $p = ".";
my $lat = $id[1] . $p . $id[2];
my $long = $id[3] . $p . $id[4];
my @stid = ( $id[0], $lat, $long);
print "@stid";
}
}
}


$idtbl keeps outputing:
KGRI
|KHSI
|KLNK
and I think it would work if it read in: KGRI|KHSI|KLNK
 
J

Jim Gibson

deadpickle said:
I want the data to be saved to a text file in this format:
KGRI
KLNK
KHSI

And then reads into the program and then run through the match string.
I cant seem to get it to work correctly. Any help?


use strict;
use warnings;
$\ = "\n";
open ID, '<', 'stations.txt' or die "cannot open 'stations.txt' $!";
open TBL, 'stidtbl.txt' or die "cannot open 'stidtbl.txt' $!";

You should check the opens for errors.
while ( my @tbl = <TBL> ) { #reads in a table of station IDs

This reads the entire file into the array @tbl in the first pass
through the while loop (hopefully, there will only be one pass). You
should read one line at-a-time into a scalar or eliminate the while
loop.
my $idtbl = join ("|", @tbl);
chop ($idtbl);

You should chomp (not chop) all of the elements of @tbl before joining
into a scalar. If you are trying to get the whole file into a scalar.
see 'perldoc -q entire' "How can I read in an entire file all at once".

chomp(@tbl);
print $idtbl;

[second loop snipped]
$idtbl keeps outputing:
KGRI
|KHSI
|KLNK
and I think it would work if it read in: KGRI|KHSI|KLNK

It is outputting that because you have embedded newlines in $idtbl
because you did not chomp the input records.
 
J

John W. Krahn

deadpickle said:
I want the data to be saved to a text file in this format:
KGRI
KLNK
KHSI

And then reads into the program and then run through the match string.
I cant seem to get it to work correctly. Any help?


use strict;
use warnings;
$\ = "\n";
open ID, '<', 'stations.txt' or die "cannot open 'stations.txt' $!";
open TBL, 'stidtbl.txt' or die "cannot open 'stidtbl.txt' $!";
while ( my @tbl = <TBL> ) { #reads in a table of station IDs
my $idtbl = join ("|", @tbl);
chop ($idtbl);
print $idtbl;
while ( my $stations = <ID> ) { #finds the lat and long of a searched
station
next unless $stations =~ /\A.{20}($idtbl)\s+(.+)/;
my @id = split (" ",$2);
if (length($id[1]) == 5) {
chop ($id[3]);
chop ($id[5]);
my $p = ".";
my $lat = $id[2] . $p . $id[3];
my $long = $id[4] . $p . $id[5];
my @stid = ( $id[0], $lat, $long);
print "@stid";
}
else {
chop ($id[2]);
chop ($id[4]);
my $p = ".";
my $lat = $id[1] . $p . $id[2];
my $long = $id[3] . $p . $id[4];
my @stid = ( $id[0], $lat, $long);
print "@stid";
}
}
}


$idtbl keeps outputing:
KGRI
|KHSI
|KLNK
and I think it would work if it read in: KGRI|KHSI|KLNK


use strict;
use warnings;

open TBL, '<', 'stidtbl.txt' or die "cannot open 'stidtbl.txt' $!";
# reads in a table of station IDs
my $idtbl = join '|', map { chomp; $_ } <TBL>;
close TBL;

open ID, '<', 'stations.txt' or die "cannot open 'stations.txt' $!";
# finds the lat and long of a searched station
while ( my $stations = <ID> ) {
next unless $stations =~ /\A.{20}($idtbl)\s+(.+)/o;
my @id = split ' ' ,$2;
splice @id, 1, 1 if length( $id[ 1 ] ) == 5;
s/\D+//g for @id;
print "$id[0] $id[1].$id[2] $id[3].$id[4]\n";
}
close ID;




John
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top