Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Perl question

Perl question
Thread Tools
Jaey
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Apr 11, 2004, 06:59 PM
 
Is there a way, in perl, to check to see if a string or variable is the same as an element in an array?
Something like:
Code:
$foo="cheese"; @bar=qw(milk beef cheese salad); if ($foo is in @bar) { print "Happy!"; }
I can do it easily with a loop, but that seems like a lot of work for such a simple thing...
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Apr 12, 2004, 01:27 PM
 
Originally posted by Jaey:
I can do it easily with a loop, but that seems like a lot of work for such a simple thing...
I am not that familiar with Perl, but looping to identify an element in an array is the most common approach (and the method the perl interpreter would employ even if it does have a shorthand way), so I would just go with that.
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Apr 12, 2004, 03:27 PM
 
*knuckles crack*
Code:
$foo="cheese"; %thisHash = ("milk", "beef", "cheese", "salad"); if (exists(%thisHash->{$foo})) { print "Happy!"; }
Ahh.. the wonders of hash arrays....

Plus, they are generally close to O:log(n) in operation, so you can have a lot of items and they are still efficient.
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Apr 12, 2004, 04:52 PM
 
Originally posted by larkost:
Plus, they are generally close to O:log(n) in operation, so you can have a lot of items and they are still efficient. [/B]
O:log(n)?




I didn't even think of hash arrays... that helps alot, thanks.
     
Basilisk
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Apr 12, 2004, 05:20 PM
 
Hashes are the best solution if you are not concerned with array order or are concerned with performance for a large number of items. If you just need a single check in a one-off script this will work just as well:

$foo="cheese";
@bar=qw(milk beef cheese salad);
if (grep {$_ eq $foo} @bar)
{
print "Happy!";
}
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Apr 13, 2004, 10:06 AM
 
Jaey: O:log(n) is a computer science term: it refers to "Big O" notation. This is a concept that tries to predict how long a process will take in very broad strokes by looking at how the program loops.

To give a simple intro into Big O with this as an example:

If you were to use the method that absmiths suggests you would have to go through every item of your array for every item you are checking. If either list gets long, this process can get very long. This is called worst case O:n (since in the worst case you will have to go through them item in the list: n).

In the case of a hash array the speed scales well below O:n, it is close to the natural log of n, so it is called O:log(n).

On short lists O:log(n) can be slightly slower than O:n, but with big lists it is much, much faster. So get into the habit of using O:log(n) algorithms whenever possible. In this specific case, it will probably be faster for short or long lists, as perl has a very efficient hash algorithm built in.
     
asidrane
Junior Member
Join Date: Jun 2001
Status: Offline
Reply With Quote
Apr 15, 2004, 05:59 AM
 
Big-O notation doesn't apply to just comp-sci. It is a mathematical term found in discrete math
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Apr 15, 2004, 12:08 PM
 
The concept should apply to any iterative process... even to folding the laundry. But it usually only gets useful when a computer is involved, since computers are so good at dong things over and over again.
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Apr 19, 2004, 03:08 AM
 
Originally posted by larkost:

Code:
$foo="cheese"; %thisHash = ("milk", "beef", "cheese", "salad"); if (exists(%thisHash->{$foo})) { print "Happy!"; }
Apparently no one has noticed this code doesn't actually work. Defining a hash this way is really setting the key-value pairs, such that:

milk => 'beef'
cheese => 'salad'

The exists() command tests for the presence of a key in the hash. Since "cheese" and "milk" are keys, this works. But it will not work for "beef" or "salad". (Try it.)

Basilisk's solution is really the proper way.

If you were planning on doing this many times, you would really want to make a hash since that'll be faster than a sequential search each time. But it has to be a hash where all your words are keys. So this code would work using a hash:

Code:
%thisHash = map{($_, 1)}("milk", "beef", "cheese", "salad"); if (exists(%thisHash->{$foo})) { print "Happy!"; }
The map function is a fancy way of making the hash:

milk => 1
beef => 1
etc.

So all your words are keys, the exists() test works, and we're all "Happy!"
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:32 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,