[VPM] programmically create keys and vals for anonhash

Abram Hindle abez at abez.ca
Sun Feb 11 18:13:57 PST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

$hash = { qw( ... ) } ;
$hash = { @tuple_array };

%hash = qw( ... );
$hash = \%hash;
$hash = { %hash };

push @arr, \%hash;
push @arr, { %hash };
Those should all work unless I forgot something, either way see perldoc
perlref for more info.

abram

Jer A wrote:
> thank you Abram for your response.
> 
> if I can create a hash like you say:
> %hash = qw( key1 value1 key2 value2 key3 value3);
> can I still access the hash with a pointer "->", if the entire %hash
> were an element in an array?
> eg.
> foreach(@arr)
> {
>  ... $_->{key1} etc?
> }
> 
> Thanks in advance,
> -Jer A.
> 
>> From: Abram Hindle <abez at abez.ca>
>> To: "\"Jer A\" (by way of Darren Duncan)"
>> <jeremygwa at hotmail.com>,        victoria-pm at pm.org
>> Subject: Re: [VPM] programmically create keys and vals for anonhash
>> Date: Sun, 11 Feb 2007 18:39:20 -0500
>>
> You can convert an array or list to hash just by assigning it:
> 
> %hash = qw( key1 value1 key2 value2 key3 value3);
> 
> Hashes can be formed from lists and array of an even number of elements
> where keys and values alternate.
> 
>     $hash{$1} = $2; #works
> 
>     push @headers,$1,$2; #works
>     ...
>     %hash = @headers;
> 
> see perldoc perldsc
> 
> abram
> 
> Jer A (by way of Darren Duncan) wrote:
>> Darren is redirecting this to the list, as my own probe succeeded.
>> ------------------
> 
>> Hello Darren, I am resending you this message directly, as there is
>> something wrong with the perl mailing-list.
> 
>> -------------------
>> Hi All,
> 
>> how do i programmically create a hash ref, without having to code the
>> key and value pairs?
>> eg.  $data = {Key1 => $val1,Key2 => $val2,Key3 => $val3} ;
>> suppose I create an Array with key and value pairs,....the number of
>> keys and values are only known at run time.
>> eg.
>> push(@HEADERS,{ KEYNAME => $1, KEYVAL => $2});
> 
>> With the information in @HEADERS, how do I use this to create a
>> hashref, with as many key/val pairs in the array, without manually
>> coding it?
> 
>> $data = { "KEY" => "VAL", ... etc };
> 
> 
>> Thanks in advance for your help.
> 
>> -Jer A.
>> _______________________________________________
>> Victoria-pm mailing list
>> Victoria-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/victoria-pm
> 
> 
>>  ** ACCEPT: CRM114 PASS osb unique microgroom Matcher **
>> CLASSIFY succeeds; success probability: 1.0000  pR: 34.7366
>> Best match to file #0 (/home/abez/crm/nonspam.css) prob: 1.0000  pR:
> 34.7366
>> Total features in input file: 3312
>> #0 (/home/abez/crm/nonspam.css): features: 378059, hits: 4066312,
> prob: 1.00e+00, pR:  34.74
>> #1 (/home/abez/crm/spam.css): features: 2082284, hits: 3381458,
> prob: 1.83e-35, pR: -34.74
> 
> 

> _________________________________________________________________
> Buy what you want when you want it on Sympatico / MSN Shopping 
> http://shopping.sympatico.msn.ca/content/shp/?ctId=2,ptnrid=176,ptnrdata=081805



> ** ACCEPT: CRM114 PASS osb unique microgroom Matcher ** CLASSIFY
> succeeds; success probability: 1.0000  pR: 24.1569
> Best match to file #0 (/home/abez/crm/nonspam.css) prob: 1.0000  pR:
> 24.1569  Total features in input file: 5208
> #0 (/home/abez/crm/nonspam.css): features: 378059, hits: 7129858, prob:
> 1.00e+00, pR:  24.16 #1 (/home/abez/crm/spam.css): features: 2082284,
> hits: 4957360, prob: 6.97e-25, pR: -24.16

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFz81lnOrfa1yW8IURAgnTAJ9ddfShGm3AEbWOx1zLeciPPmK89ACfcvHt
Tf4Nhg/ASMwFBNXwayI1WFo=
=lAa7
-----END PGP SIGNATURE-----


More information about the Victoria-pm mailing list