On Jan 22, 2008, at 5:15 p, Barry Brevik wrote:

> I'm working on a project that receives an array of data that looks  
> like
> this (example):
>
>   @fieldspecs = qw
>   (
>     JobNum    14
>     ProjectID 25
>     PartNum2  50
>     ProdQty13 15.2
>   );
>
> The array is a definition of fields in a file; the fieldname  
> followed by
> the field width. The fieldname frequently contains digits. When I get
> the data, it is text so I put it in an array (like you see above)
> because it is important to reference the fieldnames in the order I
> receive them. The array ALWAYS has a fieldname followed by the width,
> the the number of elements is always divisible by two.
>
> However, I also want to access the data as a hash so that for a given
> fieldname I can easily grab the width.
>
> It is easy enough to convert this array to a hash, but...
>
> ...is there a Perl-ish way to either ram the *fieldnames only* into a
> new array, OR, delete the field widths from the array leaving only the
> fieldnames??
>
>

assuming you want the field names to end up in @names:

my @names = grep { ! m/^\d+$/ } @fieldspecs; # works if your  
fieldnames are never *all* numeric

or

my %names = (@fieldspecs);
my @names = keys %names; # works if the files are *always* evenly paired

--Chris
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to