Here's my string:
NANA TEKA KAOE FLASK LSKK
How do I make it so that it'll look like this:
HASH = {NANA => undef, TEKA => undef, KAOE => undef, ...
Of course I could always split this into an array first
then loop through each value then assign them as hash
keys... but If there's a shorter/simpler way to do that?
Thanks in advance!
You can split the string and use a map to generate the output hash.
my $string = "NANA TEKA KAOE FLASK LSKK";
my %hash = map { $_ => undef } split(/\s/, $string);
@hash{ split /\\s+/, $string } = ();
I doubt if this is the most succinct way to do it, but it seems to work:
use warnings;
use strict;
my $string = "NAN TEKA KAOE FLASK LSKK";
my %hash = map { ($_ => undef) } split /\s+/, $string;
foreach my $key (keys %hash)
{
printf "$key => %s\n", (defined($hash{$key})) ? $hash{$key} : "undef";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.