What is wrong with this statement in Perl?

print "$_", join(',',sort keys %$h),"\n";

This gives me the error below:

Use of uninitialized value in string at missing_months.pl line 36.
1,10,11,12

this print statement is present in a for loop, as shown below:

foreach my $num ( sort keys %hash )
{
        my $h = $hash{$num};
        print "$_", join(',',sort keys %$h),"\n";
}
+3
source share
1 answer

No need for "$_". This line should be:

print join (',' , sort {$a <=> $b} keys %$h),"\n";

While $_viewed as the default iterator in loops forand foreach(see perlvar ), you have already assigned the iterator variable as $num.

Here's how to use it $_on one line:

print join(',', sort { $a <=> $b } keys %{$hash{$_}}),"\n" foreach keys %hash;

On a side note ...

sort , , '10' '2'. , (?), { $a <=> $b }.

+14

Source: https://habr.com/ru/post/1743795/


All Articles