in reply to Re: Create a hash whose keys and values come from a given array
in thread Create a hash whose keys and values come from a given array
Nice. But I would just use /\D/ to detect a non-numeric array element. No need to mess with automatically-generated classes:
# no "use autobox::universal" # ... for (@array) { if (/\D/) { $key=$_; } else { push @{$hash{$key}},$_; } } # ...
Alternatively, use looks_like_number() from Scalar::Util that wraps the Perl API function of the same name:
# ... use Scalar::Util qw( looks_like_number ); # ... for (@array) { if (looks_like_number($_)) { $key=$_; } else { push @{$hash{$key}},$_; } } # ...
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Create a hash whose keys and values come from a given array
by clueless newbie (Curate) on Sep 07, 2017 at 11:19 UTC |