Regex Solution
Solution 1: Keep everything (vol7ron's emailed solution)
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
sub main{
my @strings = (
'10 NE HARRISBURG'
, '4 E HASWELL'
, '2 SE OAKLEY'
, '6 SE REDBIRD'
, 'PROVO'
, '6 W EADS'
, '21 N HARRISON'
, '32 SAN FRANCISCO'
, ''
, '15 NEW YORK'
, '15 NNW NEW YORK'
, '15 NW NEW YORK'
, 'NW NEW YORK'
);
my %hash;
my $count=0;
for (@strings){
if (/\d*\s*[NS]{0,2}[EW]{0,1}\s+/){
# if there was a speed / direction
$hash{$count}{wind} = $&;
$hash{$count}{city} = $';
} else {
# if there was only a city
$hash{$count}{city} = $_;
}
$count++;
}
print Dumper(\%hash);
}
main();
Solution 2: Strip off what you don't need
#!/usr/bin/perl -w
use strict;
sub main{
my @strings = (
'10 NE HARRISBURG'
, '4 E HASWELL'
, '2 SE OAKLEY'
, '6 SE REDBIRD'
, 'PROVO'
, '6 W EADS'
, '21 N HARRISON'
, '32 SAN FRANCISCO'
, '15 NEW YORK'
, '15 NNW NEW YORK'
, '15 NW NEW YORK'
, 'NW NEW YORK'
);
for my $elem (@strings){
$elem =~ s/\d*\s*[NS]{0,2}[EW]{0,1}\s+(\w*)/$1/;
}
$"="\n";
print "@strings\n";
}
main();
Update:
Making the changes with vol7ron's suggestion and example, using the repetition operator worked. This will strip off leading digits and the direction and won't break if the digits or direction (or both) are missing.