in reply to Hash asignement with RE and map()

If I'm right, you've got some string which parses out into an ID, then a series of fields. You want to create a hash with key of ID and value being some hashref, pointing to a hash with key of name/email/etc and value being the value.

The basic code for this would be something like:

($ID,$name,$email,$title,$date,...) = /(ID)(name)(email)(title)(date)( +...)/; $hash{$ID}{name} = $name; $hash{$ID}{email} = $email; ...

Now, what you're doing is, essentially, assigning a hash slice. Now, if you don't know, a hash slice is done something like:

my @keys = ('a', 'b', 'c'); my @vals = (1, 2, 3); my %hash; @hash{@keys} = @vals;

At this point, $hash{a} == 1, etc.

For a hash of hashes, I think the syntax is something like:

my @keys = ('a', 'b', 'c'); my @vals = (1, 2, 3); my %hash; @($hash{key}}{@keys} = @vals;

But, please test this out.

To marry this with the code you have right now, I'd do something like:

my @keys = ('name', 'email', 'title', ...); ($ID, @vals) = /(ID)(name)(email)(title)(date)(...)/; @{$hash{$ID}}{@keys} = @vals;

Replies are listed 'Best First'.
Re: Re: Hash asignement with RE and map()
by bobione (Pilgrim) on Jul 03, 2001 at 23:02 UTC
    Thank you for your explanation, it will be usefull.
    But bbfu got the hint ;)

    Thanks all ! This make it a bit clearer now.

    BobiOne KenoBi ;)