sub chain {
return @_ > 1 ? { shift() => chain(@_) } : shift();
}
If there are two or more items in the @_ function argument
array (@_ > 1), { shift() => chain(@_) } executes.
shift takes the top argument off of @_ and makes it a
hash key with the value of whatever the call to chain(@_)
returns. Note that chain(@_) is called with whatever remains
in the @_ array after one item has been removed by the
shift call. This key/value pair is then returned within an
anonymous hash reference.
If there are fewer than two items in the @_ array, the
call to chain(@_) simply returns the top item in the array.
Note that this does not "properly" handle the case in which the
@_ array is empty. What is "proper" handling in this case (if
it can even arise)? Only you can figure this
out.
Win8 Strawberry 5.8.9.5 (32) Thu 05/13/2021 15:26:20
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
my %hash;
# my ( $top, @rest ) = split /,|\n/; # FIXME faking CSV for testing
my ($top, @rest) = qw(foo);
$hash{$top} = chain(@rest);
use Data::Dump 'dd'; dd \%hash;
sub chain {
return @_ > 1 ? { shift() => chain(@_) } : shift();
}
^Z
{ foo => undef }
... remove the first row so the header doesn't do what the rest of
the hash is doing.
One might do the following:
open(my $input, '<:utf8',"input.csv") or die;
<$input>; # read and discard one line/record
(I think Text::CSV can do this for you (it does just
about everything else :), but you'll have to check for yourself.)
Give a man a fish: <%-{-{-{-<
|