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.


In reply to Re: Perlish way of doing this by Random_Walk
in thread Perlish way of doing this by mkirank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.