Help separating arrays?
am 23.01.2008 00:15:20 von Barry Brevik
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??
Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help separating arrays?
am 23.01.2008 00:24:12 von Chris Prather
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help separating arrays?
am 23.01.2008 00:36:47 von Mike Gillis
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??
One way you could do it is by using a counter variable inside of a grep
block, like so:
my $n;
my @fieldnames = grep { $n++ % 2 == 0 } @fieldspecs;
You mentioned that your project "receives" the data, so I wonder if
there's a point in your program where you're stuffing it into the
@fieldspecs array.
If there is, you could maintain two arrays:
my $input = "JobNum 14\n ProjectID 25 \n PartNum2 50\n";
my @names;
my @widths;
foreach (split /\n/, $input) {
my ($name, $width) = /(\w+)\s+([\d\.]+)/;
push @names, $name;
push @widths, $width;
}
Then, the indexes for the names and widths will match up, and you can do
things like
for my $i (0..$#names) {
my $name = $names[$i];
my $width = $widths[$i];
# do whatever here
}
Chris Prather wrote:
> my %names = (@fieldspecs);
> my @names = keys %names; # works if the files are *always* evenly paired
The problem with this is that it doesn't preserve order.
$ perl -le 'my %names = qw(a 1 b 1 c 1); print join(", ", keys %names);'
c, a, b
Hope that gives you some options!
--
Mike Gillis
ActiveState Software
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Help separating arrays?
am 23.01.2008 00:54:11 von Jan Dubois
On Tue, 22 Jan 2008, Chris Prather wrote:
> my %names = (@fieldspecs);
> my @names = keys %names; # works if the files are *always* evenly paired
There is no need for the temporary variable (unless you can make some
other use of the hash anyways); you can just create an anonymous hash
reference:
my @names = keys %{{@fieldspecs}};
But, as Mike already mentioned, this will not preserve the original
order of the field names.
Cheers,
-Jan
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Help separating arrays?
am 23.01.2008 10:52:14 von Brian Raven
Mike Gillis <> wrote:
> Chris Prather wrote:
>> my %names =3D (@fieldspecs);
>> my @names =3D keys %names; # works if the files are *always* evenly
>> paired
> =
> The problem with this is that it doesn't preserve order.
> =
> $ perl -le 'my %names =3D qw(a 1 b 1 c 1); print join(", ", keys
> %names);' =
> c, a, b
You can preserve the order with Tie::IxHash, e.g.
--------------------------------------
use strict;
use warnings;
use Tie::IxHash;
my @fieldspecs =3D qw
(
JobNum 14
ProjectID 25
PartNum2 50
ProdQty13 15.2
);
tie my %hash, "Tie::IxHash", @fieldspecs;
my @names =3D keys %hash;
print "@names\n";
--------------------------------------
.... or alternatively, if you don't want to use a hash, just extract the
array entries with even indices:
my @names =3D @fieldspecs[grep {$_ % 2 == 0} 0..$#fieldspecs];
print "@names\n";
HTH
-- =
Brian Raven =
==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================
The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =
Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.
L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.
Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs