develooper Front page | perl.beginners | Postings from March 2002

Grep (WAS RE: searching an array)

Thread Next
From:
Timothy Johnson
Date:
March 29, 2002 16:24
Subject:
Grep (WAS RE: searching an array)
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D50025811FF@SMILEY

I've heard that grep is fairly resource-intensive.  Is there any truth to
the rumors?  Is there a time where using grep will be faster than cycling
through an array?

-----Original Message-----
From: Wagner-David [mailto:Wagner-David@vikingfreight.com]
Sent: Friday, March 29, 2002 4:20 PM
To: 'Mahendra Thacker'
Cc: beginners@perl.org
Subject: RE: searching an array


	Here is one shot:

#!perl -w

my $a = 5;
my @arr = qw( 7 9 3 6 5);

my @MyHits = grep(/$a/ , @arr );
if ( @MyHits ) {
   print "Found $a in the list \n";
} else {
   print "Did not find $a in the list\n";
}

Made minor changes: used warnings, my.

Wags ;)

-----Original Message-----
From: Mahendra Thacker [mailto:mnt_cal@snet.net]
Sent: Friday, March 29, 2002 16:09
To: beginners@perl.org
Subject: searching an array


Hi,

A question from a relative newbie.

How does one search an array of numbers for a number?
(without using foreach; is there any function?)

Say, I want to do something like this:

==

$a = 5;
@arr = ( 7 9 3 6 );

if ( seach( $a, @arr ) ) {
   print "Found $a in the list \n";
} else {
   print "Did not find $a in the list\n";
}

==

TIA,

Mahendra Thacker




-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About