[ILUG] perl: How to init a hash from a command?

Pádraig Brady P at draigBrady.com
Thu Dec 11 15:02:21 GMT 2008


Paul Mullen wrote:
> Pádraig Brady wrote:
>> For my sins, I've now to maintain a bit of perl.
>> If I have a `command` that outputs:
>>   name1=value1
>>   name2=value2
>> How do I init a hash with those names and values?
>> I.E. how do I do the following python in perl?
>>
>> command=dict(nv[:-1].split('=') for nv in os.popen("command"))
>>
>> cheers,
>> Pádraig.
>>   
> open F, "command |";
> 
> my %hash
> while(<F>) {
> my ($name, $value) = split(/=/, $_);
> $hash{$name} = $value;
> }

Thanks for that.

Someone mentioned to me years ago that perl had 'map',
and a quick google confirms it:
http://www.webmasterworld.com/forum13/492.htm
So I'll probably cook something up with that.

cheers,
Pádraig.



More information about the ILUG mailing list