perl - Filtering According To Second Element in a "Tuple" -


i trying zip list sum of each tuple within list, , filter tuples sum higher 0 in list. @ point, using test code given below, nothing prints after line "---- print random tuples above 0". missing?

 sub filter_random_4_toops_by_sum {  ($toops, $thresh) = @_;  @toops1 = ();  @toops1 = $toops;  @sorted_toops = ();  @sortedreturn = ();  @filtered_toops = ();  @sorted_toops = map{[$_, sum(@$_)]} @toops1;  @filtered_toops = grep {$_[1]> $thresh} @sorted_toops;  @sortedreturn = map{$_->[0]} @filtered_toops;  return \@sortedreturn; } 

the test code:

sub test_step_4 {  my($sn)= 1;  $toops = gen_random_4_toops(1, 100, 5);  print "---- random 4-toops:\n";  foreach(@{$toops}) { print "toop $sn:\t(@{$_});\n , sum = " . sum(@{$_}) . "\n"; $sn++; }  $thresh = 55;  print "\n---- random 4-toops filtered sum above $thresh:\n";  $filtered_toops = filter_random_4_toops_by_sum($toops, $thresh);  $sn = 1;  foreach(@{$filtered_toops}) { print "toop $sn:\t(@{$_}); sum = ". sum(@{$_}) . "\n"; $sn++;  } } 

test code should output:

---- random 4-toops: toop 1: (49 49 4 64); sum = 166 toop 2: (-2 16 57 76); sum = 147 toop 3: (-94 93 -48 85); sum = 36 toop 4: (19 -47 14 38); sum = 24 toop 5: (-57 80 -60 -35); sum = -72 ---- random 4-toops filtered sum above 0: toop 1: (19 -47 14 38); sum = 24 toop 2: (-94 93 -48 85); sum = 36 toop 3: (49 49 4 64); sum = 166 toop 4: (-2 16 57 76); sum = 147 

equivalent of i'm trying in python (working):

def filter_random_4_toops_by_sum(toops, thresh):     summit = 0     s = []     in toops:         # pdb.set_trace()         summit = 0         d in xrange(0,4):             summit += i[d]         s.append(summit)     # pdb.set_trace()     = zip(toops, s)     filtertog = [i in if i[1] > 0]     toops = [x[0] x in filtertog]     return toops 

@toops1 has 1 element.

@toops1 = $toops; 

should be

@toops1 = @$toops; 

but why not use @$toops directly instead of copying elements?

sub filter_random_4_toops_by_sum {    ($toops, $thresh) = @_;    @sorted_toops = map { [ $_, sum(@$_) ] } @$toops;    @filtered_toops = grep { $_[1] > $thresh } @sorted_toops;    @sortedreturn = map { $_->[0] } @filtered_toops;    return \@sortedreturn; } 

or even

sub filter_random_4_toops_by_sum {    ($toops, $thresh) = @_;    return [       map { $_->[0] }          grep { $_[1] > $thresh }             map { [ $_, sum(@$_) ] }                @$toops    ]; } 

or even

sub filter_random_4_toops_by_sum {    ($toops, $thresh) = @_;    return [ grep { sum(@$_) > $thresh } @$toops ]; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -