Perl - Trying to use sort method -
i don't know exact word i'm trying achieve in perl.
i have variable named $var
my $var = "10 60 20 70 30 60 30 50 50 40 40 30 40 20 50 10 60";
this dynamic value. @ moment predefined make easy understand.
then split $var
my $var1 (split //, $var);
and point dont know im talking about.
i want sort $var1
this
30 40 50 10 20 30 40 50 60 10 20 30 40 50 60 70
is there way achieve result?
i tried researching sort none of results found covered issue. if used method 10 20 30... , on.
the best sense can make of question want hierarchy of lists — each containing duplicates next list in sequence. there 3 occurrences of 60
in input , 2 in output.
this solution works pushing each value onto first element of @groups
doesn't contain value. once items placed, array reversed , each sub-array sorted in numerical order
use strict; use warnings; use list::util 'any'; $var = "10 60 20 70 30 60 30 50 50 40 40 30 40 20 50 10 60"; @var = split ' ', $var; @groups; $item ( @var ) { $i = 0; ++$i while { $item == $_ } @{ $groups[$i] }; push @{ $groups[$i] }, $item; } @groups = map [ sort { $a <=> $b } @$_ ], reverse @groups; use data::dump; dd \@groups;
output
[ [30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60, 70], ]
Comments
Post a Comment