Hello
The easiest way to do this is using hash slices. Let me explain.
my %hash = (); # creates an empty hash
$hash{name} = 'Aziz'; # set the name
$hash{id} = 512; #set the id
$hash{bdate} = '12/3/1976/'; #set the bdate
or, you can do this:
%hash = (name => 'Aziz', id => 512, bdate => '12/3/1976');
but did you know you can do this:
@hash{'name','id','bdate'} = ('Aziz', 512, '12/3/1976');
or if you already have the headers:
my @columns = qw/name id bdate/;
@hash{@columns} = ('Aziz', 512, '12/3/1976');
So, to do your task, first get the first line, chomp it, and split it to get
the columns order.
Then foreach line, create a new hash, and assign the values (from split) to the
keys (from @cols).
open F, "<data.txt" or die $!;
$_ = <F>;
chomp;
my @cols = split /;/;
my @rows;
while(<F>){
chomp;
my %row;
@row{@cols} = split /;/;
push @rows, \%row;
}
Hope this helps,
Aziz,,,
On Mon, 09 Jul 2001 12:26:48 +0100, [EMAIL PROTECTED] said:
>
> Tip request...
>
> I have a text file produced by another system. The fields are semi-colon
>delimited, and the first line is a list of the fields, a header record, also
>semi-colon delimited.
>
> The trick with this file is that each time the file is produced, the fields are not
>necessarily in the same order (yeah I know; who can guess where the file comes from?).
>
> All the fields are present each time, just in different orders, and the header
>record is the key to the order.
>
> Has anyone any Perl-efficient ways of pulling the header record out into a
>structure that makes it easy to get the right fields for munging?
>
> I can think of a number of ways I would do this in other languages, but any tips on
>the ways of Perl would be excellent.
>
> Thanks,
>
> Paul.
>
> ------------------------------------------------
> Global WebMail -
> Delivered by Global Internet www.global.net.uk
> ------------------------------------------------
>
>
>
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com