in reply to Perlish way of doing this

You're storing your data in the wrong structures.

You never use the values in the hash %a; you never use it's lookup ability; and you want to iterate it in order.

Your arrays @a1 and @a2:

  1. you want to use as lookup table and have to iterate the whole thing each time with grep.
  2. Dont care about the order, only the presence.
  3. Iterate, only for the greps, never for the ordering.

By reversing the type of structures used you get:

#! perl -slw use strict; my %a=( AA=>'Y', BB=>'Y', CC=>'Y', DD=>'Y', EE=>'Y', FF=>'Y' ); my @a1=('AA','DD','EE'); my @a2=('AA','BB','CC','FF'); my @a = sort keys %a; my %a1; @a1{ @a1 } = (); my %a2; @a2{ @a2 } = (); print join ',', map{ exists $a1{ $_ } ? $_ : '##' } @a; print join ',', map{ exists $a2{ $_ } ? $_ : '##' } @a; __END__ [11:44:17.92] P:\test>413151 AA,##,##,DD,EE,## AA,BB,CC,##,##,FF

Examine what is said, not who speaks.
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Perlish way of doing this
by kappa (Chaplain) on Dec 08, 2004 at 12:09 UTC
    Small suggestion to reduce redundancy:
    for my $h (\%a1, \%a2) { print join ',', map{ exists $h->{ $_ } ? $_ : '##' } @a; }
    Knitpicking is the only way to perfection.
    --kap

      You're right!

      ... except now that you have to explain not only why the OP should be using an array instead of a hash, and hashes instead of arrays, but also why you're escaping your percent signs and why your $h is firing an arrow at the head of a cartoon character with a one eye closed and a dollar sign in the other :)


      Examine what is said, not who speaks.
      "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
      "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

        That's why I commented on your comment and did not write a full solution.

        And I'm gonna save your quote about the cartoon character :)) I laughed. Thanks :)

        --kap