in reply to Perlish way of doing this

how about

print "AA,##,##,DD,EE,##\nAA,BB,CC,##,##,FF\n" # ;-)
What I am saying is that it is obvious what you are doing but why ? Which parts of this are external inputs and what can we play with ? Why do you assign the value 'Y' to all the values of a hash and later sort the keys ? An array would look to be a more obvious solution or even just do this

#!/usr/local/bin/perl -w use strict; my @a1=('AA','DD','EE'); my @a2=('AA','BB','CC','FF'); my(@tmp1,@tmp2); foreach my $xx ('A'..'F') { $xx=$xx x 2; $a = grep (/$xx/, @a1); $b = grep (/$xx/, @a2) ; if (($a && $b) || (!$a && !$b)) { push (@tmp1,$xx); push (@tmp2,$xx); } elsif ($a && !$b) { push (@tmp1,$xx); push (@tmp2,'##'); } else { push (@tmp1,'##'); push (@tmp2,$xx); } } print join(',',@tmp1); print "\n"; print join(',',@tmp2); print "\n";

Why the ddoouubbllee letters ? Can we do it all in single letters and double them up at print time ? Will one of the values always be present in either @a1 or @a2 ? Why not put these into hashes to save that greping ?

#!/usr/local/bin/perl -w use strict; my (%a1, %a2, @tmp1,@tmp2); $a1{$_}=1 for ('AA','DD','EE'); $a2{$_}=1 for ('AA','BB','CC','FF'); foreach my $xx ('A'..'F') { $xx=$xx x 2; if (($a1{$xx} && $a2{$xx}) || (!$a1{$xx} && !$a2{$xx})) { push (@tmp1,$xx); push (@tmp2,$xx); } elsif ($a1{$xx}) { push (@tmp1,$xx); push (@tmp2,'##'); } else { push (@tmp1,'##'); push (@tmp2,$xx); } } print join(',',@tmp1); print "\n"; print join(',',@tmp2); print "\n";

This is not as simple as some of the examples above but then this one handles the case where neither a1 or a2 contains the value and correctly puts XX into both output streams.

Cheers,
R.

Replies are listed 'Best First'.
Re^2: Perlish way of doing this
by Random_Walk (Prior) on Dec 08, 2004 at 14:50 UTC

    Here is an improvement on the above. I do not use map to fill the two tmp arrays as they would need to be itterated over again to find the case where both are blank. May as well only loop once (I added GG to test this case too).

    #!/usr/bin/perl -w use strict; my (%a1, %a2, @tmp1,@tmp2); $a1{$_}=1 for ('AA','DD','EE'); $a2{$_}=1 for ('AA','BB','CC','FF'); foreach my $xx ('A'..'G') { $xx=$xx x 2; push @tmp1,($a1{$xx} ? $xx : 'XX'); push @tmp2,($a2{$xx} ? $xx : 'XX'); $tmp1[-1]=$tmp2[-1]=$xx if $tmp1[-1] eq $tmp2[-1] } print join(',',@tmp1); print "\n"; print join(',',@tmp2); print "\n";

    Cheers,
    R.