On Fri, 22 Mar 2002, James Linden Rose, III wrote:
> Let me clarify my question a little.
>
> I have a data file whose field order is fluid, but whose names are
> known, and whose records are delimited by returns, and whose fields
> are delimited by tabs. The data file is stored in say @File. But
> the first record contains the one word name of the field. So I build
> an array - let's call it @rray = split (/\t/, $File[0]); , that
> contains all of the field names in the current order of the file.
>
> Then when I want to build some output based on record $x of this
> file, I create say:
>
> @Record = split (/\t/, $File[$x]);
>
> Now I want to organize my logic by being able to call up say
> $Record[$address], or $Record[$phone_num], etc etc.
>
> To do that I need $address to contain the position of the "address"
> field, gleaned from the current order of the fields contained in
> @rray. So if @rray currently looks like qw (address phone_num fax
> note), I need:
>
> $address = 0;
> $phone_num = 1;
> $fax = 2;
> $note = 3;
>
> So later I can say things like
>
> print "Record $x's Phone Number is: $Record[$phone_num]\n";
>
> But, when I run this to get there:
>
> my $i = 0;
> foreach (@rray) {
> $$_ = $i
> $i++;
> }
Why not use a hash for each record
@fields = (qw/address phone_num fax note/);
@record{@fields} = split( /\t/, ... );
and then you can use
$record{'address'} etc.
Mike
--
[EMAIL PROTECTED] | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
[EMAIL PROTECTED] | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.starnix.com/ | 75D2 9EC4 C1C0 0599 13DA