Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; my $names; while(<DATA>){ if(/{(.*)}/){ $names = $1; } } __DATA__ {NAME} {AGE} {SEX} {ADDRESS}
How to convert $names as hash map and assign the values to it
NAME should be john
AGE should be 35
SEX should be M
{ADDRESS} should be USA
output should be John,35,M,USA

Replies are listed 'Best First'.
Re: Assign value
by ikegami (Patriarch) on Nov 04, 2009 at 15:38 UTC
    $names{NAME} = 'john';
    It's very weird that variable named %names contains information about a person.
Re: Assign value
by bichonfrise74 (Vicar) on Nov 04, 2009 at 16:33 UTC
    Where will you get the values for your hash keys? I think this is what you want.
    #!/usr/bin/perl use strict; use warnings; my %record_for; while (<DATA>) { chomp; my ($key, $val) = /\{(\w+)\}\s*?(\w+)/; $record_for{$key} = $val; } print join ',', map { $record_for{$_} } keys %record_for; __DATA__ {NAME} John {AGE} 35 {SEX} M {ADDRESS} USA
      Note that the statement
          map { $hash{$_} } keys %hash;
      produces exactly the same list as
          values %hash;
        Thanks. I was thinking about what the keyword for 'values' was but just totally forgot about it. Anyway, thanks again.