On Tue, Sep 23, 2003 at 08:49:11PM +0530, Pandey Rajeev-A19514 wrote:
> Hi,
> 
> I have a list like
> 
> @list = ( "key1: Vlan1 :0989\n"
>       "key2: Vlan2 :0989\n"
>       "key3: Vlan3 :0989\n"
>       "key4: Vlan4 :0989\n");
> 
> I wanted to make a hash with keys as key1, key2 , key3, key4.
> 
> I want to write something like this :
> **********************************************
>   my @keys, @values, @key_to_value;
>   (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
>   @[EMAIL PROTECTED] = @values;
> 
> But the problem is the map function just creates one list i.e. @keys .. so I am not 
> able to make the hash.
> 
> ie .. I want to extract @keys and @values in one go and then use the HASH SLICE to 
> get the desired result.
> 
> Can some one help me. The only this is I don't want to extract @keys and @values 
> separately.
> 
> Regards
> Rajeev


Hi Rajeev,

Sounds like you want to create the hash, the @keys list, and the
@values list in one pass, right?  Ok, this should do it:

my (@keys, @values, %hash);
my @list = ( "key1: Vlan1 :0989\n",
             "key2: Vlan2 :0989\n",
             "key3: Vlan3 :0989\n",
             "key4: Vlan4 :0989\n",
             );
do { /(key\d+): (\w+).*?$/; push @keys, $1; push @values, $2; $hash{$1}=$2;  } for 
@list;



You can't use map here, because map always returns a single list, and
you are trying to build up multiple lists at once.  (Or, rather, you
COULD use it, instead of the for, but it would be pointless as you
would simply be throwing out the list that it returns.)

(Oh, and you needed commas after your list elements.)


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to