in reply to Looking for exact match in an array

saintmike answered your question. But by the way, it looks like you want a data structure you can easily and efficiently use to determine membership of a particular value. In this case, a hash can be useful:

my %sheets; [...] # The next three methods are equivalent. Pick whichever you like most. # 1 if (not exists $sheets{$machine_sheetname}) { $sheets{$machine_sheetname} = 1; # "1" is just a dummy existenc +e flag } # 2 $sheets{$machine_sheetname} = 1 unless $sheets{$machine_sheetname}; # 3 $sheets{$machine_sheetname} ||= 1;

Then, when you need all the sheets, use this expression:

keys %sheets;

The prime limitation of this approach, however, is that hashes are not sorted. If the order of your sheets matter, this may not be appropriate after all, or, if you do want to use them, you need to use something like Tie::IxHash.

Replies are listed 'Best First'.
Re^2: Looking for exact match in an array
by revdiablo (Prior) on Dec 10, 2004 at 17:36 UTC

    Nice reply, but there are a few things I would do differently. Consider this another "[p]ick whichever you like most" post.

    # your number 1 uses "if (not ...)" # which is equivalent to "unless (...)" # 4 unless (exists $sheets{$machine_sheetname}) { $sheets{$machine_sheetname} = 1; } # but I'm not really sure why you're checking for existence at all. # just incrementing the value is the simplest approach. # 5 $sheets{$machine_sheetname}++;
      Personally, I like unless almost only in statement modifier position, where the expression is simple. This is a matter of taste and I'm not arguing that this is better. (Well, I'm almost certain to wrinkle my nose at *complex* expressions that start with unless.) So I'd much rather do #2 than #4. You'll notice that I included the existence test only in #1, which was the long form.

      #5 is fine and dandy, and has the additional feature of giving you a count of seen instances. I do this sometimes :) but where what I'm implemented is strictly a set, sometimes I prefer decidedly not to have it, just to make clear that the only information this hash provides is membership. YMMV etc.