On May 30, Wilson, Tom said:

>My two cents.... sometimes getting the answer is more important that
>learning it right away.  There are many times in the various languages that
>I've worked with that I've gotten the answer and later on remembered the
>solution and incorporated it into something new once I understood it.  

I don't mind giving answers as long as you know how to apply them if
they're too generic, and you know HOW I'm solving your problem.

Take the common question of "how can I get unique elements from a
list?"  You can do it slowly, by using two arrays:

  ORIG: for my $o (@original) {
    for (@unique) {
      next ORIG if $_ eq $o;
    }
    push @unique, $o;
  }

Or even worse[1], using grep:

  for my $o (@original) {
    push @unique, $o unless grep $_ eq $o, @unique;
  }

The idiomatic Perl approach is to use a hash; store the elements
of the list as the keys in the hash, since a hash's keys are unique.
There are several approaches, really:

  # works because $x++ is POST-increment, so it returns $x first, and THEN
  # increments it.  so if !$seen{$_}++ is true, that means $seen{$_}
  # wasn't true (because the key $_ hasn't been seen yet)

  @unique = grep !$seen{$_}++, @original;


  # this is pretty much the same as above, but more drawn out

  for (@original) {
    next if $seen{$_};
    $seen{$_} = 1;
    push @unique, $_;
  }


  # hash slices are nifty-keen, but this approach loses the order of the
  # elements (if that was important to you)

  @seen{@original} = ();
  @unique = keys %seen;

See, I would make sure you knew what I was giving you.  There's nothing
worth than spoonfeeding a baby gruel, if neither the parent nor the child
knows what's inside (and it's better if the baby likes it).

In addition, if you said "no, japhy, I meant, I don't want to keep the
element if it appears more than once," then I would expect you to be able
to craft a working solution from the ones shown.

  @original = (2,5,4,2,6,5,3,7,6);
  # solution...
  @unique = (4,3,7);

If you're curious how to do that, let me give you an hint:  the first
solution I showed for unique-ness can be molded slightly to provide a
count of each element:

  $count{$_}++ for @original;
  
That's all for now.



[1] The grep() method is worse because it doesn't stop after finding a
match, it goes through the entire array.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **

Reply via email to