[EMAIL PROTECTED] wrote:

> Thanks Jeff.  I hope to try this out later today.  I thought I had the
> solution earlier this morning, but I ran into a problem.  I hope this will
> solve it !  Thanks again.
>
> >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.
> >
> >Anyone know how to do that ?
>
> Well, there are many ways.  You could split the string on whitespace,
> remove the first and last elements, and join the others with spaces:
>
>   for (@data) {
>     my @fields = split;
>     shift @fields;
>     pop @fields;
>     push @cities, "@fields";  # "@array" = join(" ", @array)
>   }

I'd vote for this one--almost.  It does the right thing with positions,
presuming that Stuart can count on the fruit type and class always being
contained in a single token.  The one thing I would do is to give the parts
meaningful names.  Unless he totally wants to discard the significant fruit
name as well as the non-informaticve class desiganation "Fruit", he might as
well preserve the information that he has available:

foreach (@data) {
    my @fields = split;
    pop @fields;  # only use void statements to get rid of garbage
    my $growing_location = {
        'fruit type' => shift @fields,
         'growing location'  =>  join @fields
    }
    push @cities, $growing_location;
}

Okay, I don't know whether these really indicate growing locations, but I am
assuming sanity here--that there is some articulable meaning to the
juxtaposition.  The identifiers in the code should communicate that meaning..
Otherwise you are throwing information away, the antithesis of the
programmer's purpose.  Besides, clearly named variables are much easier to
debug.

Joseph




-- 
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