Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The intended result for each sample is like this:# col1 col2 index my $sample1 = ['str1 str2 1', 'str3 str4 2', 'str5 str6 3', 'str7 str8 4', 'str9 str10 5']; my $sample2 = ['str1 str2 0', 'str3 str4 4']; my $sample3 = ['str1 str2 3', 'str3 str4 4'];
The flow/logic of the algo is like this:$result_spl1 = [ 'str1', 'str3', 'str5', 'str7', 'str9', 'str10' ]; $result_spl2 = [ 'str1', 'str2', 'str3', 'str4', ]; $result_spl3 = [ 'str1', 'str3', 'str4', ];
use Data::Dumper; get_column($sample1); sub get_column { my $ar = shift; my $all; foreach my $str ( @{$ar} ) { my @ar = (split " ",$str)[0,1,2]; push @$all, [ @ar ]; } my @clean; my $flag = $all->[0][2]+1; my $idx; my $diff; push @clean, $all->[0][0],$all->[0][1]; foreach my $i ( 1 .. @$all-1 ) { $idx = $all->[$i][2]; $diff = abs($idx-$flag); if ( $diff == 1 ) { push @clean, $all->[$i][1]; } else { push @clean, $all->[$i][0],$all->[$i][1]; } $flag = $idx; } print Dumper \@clean ; return ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple Logic Problem with Perl
by CountZero (Bishop) on Oct 19, 2005 at 10:27 UTC | |
by Anonymous Monk on Oct 19, 2005 at 12:14 UTC | |
|
Re: Simple Logic Problem with Perl
by tphyahoo (Vicar) on Oct 19, 2005 at 14:31 UTC | |
|
Re: Simple Logic Problem with Perl
by jdporter (Paladin) on Oct 19, 2005 at 14:21 UTC | |
by Anonymous Monk on Oct 19, 2005 at 16:43 UTC | |
|
Re: Simple Logic Problem with Perl
by Anonymous Monk on Oct 19, 2005 at 09:39 UTC | |
by jdporter (Paladin) on Oct 19, 2005 at 14:47 UTC | |
|
Re: Simple Logic Problem with Perl
by pajout (Curate) on Oct 19, 2005 at 09:49 UTC | |
|
Re: Simple Logic Problem with Perl
by sauoq (Abbot) on Oct 19, 2005 at 20:27 UTC | |
|
Re: Simple Logic Problem with Perl
by JamesNC (Chaplain) on Oct 19, 2005 at 23:19 UTC |