On Fri, Jan 23, 2004 at 12:01:13AM -0500, [EMAIL PROTECTED] ([EMAIL PROTECTED]) wrote:
> This newbie needs help with a regex.  Here's what the data from a text 
> file looks like. There's no delimiter and the fields aren't evenly spaced 
> apart. 
> 
> apples          San Antonio      Fruit
> oranges Sacramento             Fruit
> pineapples     Honolulu         Fruit
> lemons    Corona del Rey       Fruit 
> 
> Basically, I want to put the city names into an array.  The first field, 
> the fruit name, is always one word with no spaces.
> 
> So, I would guess that the regex needs to grab everything after the first 
> word and before the beginning of Fruit.  Then strip out all the spaces. 
> 
> Or grab the beginning of the second word until the beginning of Fruit. 
> Then strip out the spaces after the city name. 
> 
> Anyone know how to do that ?

I'm not that experienced with Perl but here is my stab at it.

#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>) {
    if ($_ =~ /^\w+\s+(.+)\s+F\w+$/) {
        push (my @array, $1);
        print "@array\n";
    }
}

__DATA__
apples          San Antonio      Fruit
oranges Sacramento             Fruit
pineapples     Honolulu         Fruit
lemons    Corona del Rey       Fruit
__END__

Not the greatest regex but it works.  I'm sure you will get better
solutions.

/^     = Beginning of line
\w+    = one or more word characters
\s+    = one or more white spaces
(.+)   = any character one or more times grouped by (), contains "city"
\s+F   = white space up to "F"
\w+$/  = one or more word characters up to end of line.

push loads @array with "$1" which snags what is in (.+) from the regex.
hth,
Kent 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to