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 Split Function 2

Status
Not open for further replies.

jmg498

Technical User
Oct 31, 2007
26
0
0
US
The following script reads a file called warning.txt and extracts a string of latitude and longitude values from the file. The format of this string is: LAT1 LON1 LAT2 LON2 LAT3 LON3 up to a maximum of 20 LAT/LON pairs.

I would like to split the string and store each latitude in its own array and each longitude in its own array. Perhaps one way to do this would be to split the whole thing into an array and then make all LATs the odd elements and all LONs the even elements.

Any suggestions and code assistance for how to use split to do this would be appreciated.

Thank you.

Code:
#!/usr/bin/perl

open (WARNING, "/home/josh/warning.txt");
@arwarn = <WARNING>;
$stwarn = join('', @arwarn);

if($stwarn =~ m/LON(.*)\nTIME/) {
	$coords = $1;
}
 
To clarify, I meant I want to store all latitudes in an array and all longitudes in an array. Not each. Sorry for the confusion.
 
Code:
my $data = 'LAT1 LON1 LAT2 LON2 LAT3 LON3 LAT4 LON4';
my @lat,@lon;

my @data = split(/\s+/, $data);

for (my $x=0; $x<$#data; $x+=2) {
	push @lat, $data[$x];
	push @lon, $data[$x+1];
}

print "Lat @lat\nLon @lon\n";


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You can also do it this way, but I'm not pretending this is faster or even just neater than the normal way...
Also to be noted that the order of data will be messed up, but pairs of LAT/LON values will stay in touch
Code:
$_='LAT1 LON1 LAT2 LON2 LAT3 LON3 LAT4 LON4';
%lonlat=split;
@lat=keys%lonlat;
@lon=values%lonlat;

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks much, guys! That really helps. I seem to be losing the last longitude value in both methods, but that may be a consequence of my previous code. I'll have to look over things in more detail later. But it definitely gets me further than I was before.

Thanks so much!
 
If your still having problems post your complete code and a chunk of your data file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
prex1 said:
Also to be noted that the order of data will be messed up, but pairs of LAT/LON values will stay in touch
It should also be noted that if you have the same LAT with multiple LONs, you'll only get the last LON.
 
That might be the problem. I need all lat/lon values to remain in tact, even if they're the same. That way when they're passed to another function that creates the shapefiles (for use in ArcGIS), it can pair the correct latitudes with the correct longitudes.

I can work on that though through the week. The suggestions here have got me much further, so with some references I should be able to fix it. A more pressing issue I came up with is with my regular expression that I'm using to extract the lat/lon values. In all cases, the values appear between the words "LON" and "TIME" in the file. But sometimes there can be a new line after the last lon, sometimes carriage returns, sometimes whitespace...it can vary. I've been playing around with different patterns with no luck. One inefficient way I guess I could handle this is through "if" statements for each condition. But if anyone has any better ideas, I'd be eternally grateful.

If it helps, the files I'm trying to grab lat/lon data from are weather warnings, such as this:
Here is the code:
Code:
#!/usr/bin/perl

open (WARNING, "/home/josh/warning.txt");
@arwarn = <WARNING>;
$stwarn = join('', @arwarn);

if($stwarn =~ m/LON(.*)TIME/) {
# I've also tried m/LON(.*)\nTIME and \r, etc and it works in some cases when those appear #
	$coords = $1;
}

@metdata = split(/\s+/, $coords);

for($x=0; $x<$#metdata; $x+=2) {
	push @lon, $metdata[$x];
	push @lat, $metdata[$x+1];
}
 
try this
Code:
my $data = 'OTHER LOCATIONS IN THE WARNING INCLUDE ROARING RIVER STATE PARK AND
TABLE ROCK LAKE.

PRECAUTIONARY/PREPAREDNESS ACTIONS...

THESE ARE DANGEROUS STORMS CAPABLE OF PRODUCING LARGE HAIL AND
DAMAGING WINDS...IF YOU ARE IN THEIR PATH SEEK SHELTER INDOORS AND
STAY AWAY FROM WINDOWS.

&&

LAT...LON 3649 9408 3681 9408 3682 9406 3682 9359
      3652 9359 3649 9360
TIME...MOT...LOC 0426Z 288DEG 31KT 3680 9399 3652 9444

$$

SCHAUMANN';



if ($data =~ m/LON(.*)TIME/s) {
	$coords = $1;
}

$coords =~ s/\s+/ /g;
$coords =~ s/^\s+//g;

my @lat,@lon;

my @data = split(/\s+/, $coords);

for (my $x=0; $x<$#data; $x+=2) {
	push @lat, $data[$x];
	push @lon, $data[$x+1];
}

print "Lat @lat\nLon @lon\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'll give this a shot when I get back tonight. Thanks so much for your help, Travis! I greatly appreciate it. You da man.
 
Travis' code modified a little bit:
Code:
my (@lat,@lon);
if ($data =~ m/LON\s*(.*)\s*TIME/s) {
	my @temp = split(/\s+/s, $1);
	for (my $x=0; $x<$#data; $x+=2) {
		push @lat, $data[$x];
	    push @lon, $data[$x+1];
	}
	print "Lat @lat\nLon @lon\n";
} else {
	print "No Match!"
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top