what efficient way rearrange array elements ordered & place corresponding index value (minus one)? $dat variable being max number of elements (which never exceeded in array may or may not present).
$dat = 14; @array = (1, 12, 14, 7, 8, 4)
in other words:
my @new_array = (1, undef, undef, 4, undef, undef, 7, 8, undef, undef, undef, 12, undef, 14);
***edit**** fuller code snippet:
foreach $auth (keys %activity) { @value = @{ $activity{$auth} }; @value = uniq @value; @value = sort @value; s/^0// @value; $count = scalar(grep $_, @value); $dat = max( @value ); @{$activity{$auth}} = @value; }
you can create new array values defined after finding maximum value in first, in new array undefine value not in first:
use strict; use warnings; use data::dumper; @array = ('1', '4', '3'); $max = (sort { $b <=> $a } @array)[0]; #should 4 print dumper(\@array); @new_arr; foreach $index (0 .. ($max - 1)) { $new_arr[$index] = ($index + 1); #array should populated, @new_arr = ('1', '2', '3', '4'); $new_arr[$index] = 'undef' unless (grep {$_ eq $new_arr[$index]} @array); #values not in original array should set undef } # @new_arr should (''1', 'undef', '3', '4') print dumper(\@new_arr);
output:
$var1 = [ '1', '4', '3' ]; $var1 = [ 1, 'undef', 3, 4 ];
Comments
Post a Comment