Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Regular Expressions

Status
Not open for further replies.

jmg498

Technical User
Oct 31, 2007
26
0
0
US
It's been 2 years since I've done any work with regex in Perl so I'm hoping someone can offer a suggestion. I'd like to save all values in a text file (that's stored in array) between two keywords: "LON" and "TIME", into variables. The problem is I'd like to save each value to its own variable.

For example, in the file I might have something like: "LAT...LON 4435 9351 4821 9238 TIME" and I'd like to have $lat1 = 4435, $lon1 = 9351, $lat2 = 4821, $lon2 = 9238, etc.

Can anyone offer any help on how I could construct this regex? It is all being read from a file called file.txt as follows:

open(FILEHANDLE, "file.txt");
@text = <FILEHANDLE>

Thank you!
 
I was thinking perhaps I could use a loop to store each value in an array, and then access the values by element. But I'd still need some help getting the right pattern.
 
You could do something like this:
Code:
$_ = 'LAT...LON 4435 9351 4821 9238 TIME';
m/LON\s+([\d ]+)\s+TIME/;
my @input = ($1 =~ m/\d+ \d+/g);
my %h;

for (my $i = 0; $i <= $#input; $i++) {
	my @temp = split(/\s+/, $input[$i]);
	$h{"lat" . ($i + 1)} = $temp[0];
	$h{"lon" . ($i + 1)} = $temp[1];
}

print "lat1: $h{lat1} - lon1: $h{lon1}\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top