Front page | perl.beginners |
Postings from March 2002
Re: Grep (WAS RE: searching an array)
Thread Previous
|
Thread Next
From:
Mahendra Thacker
Date:
March 30, 2002 07:43
Subject:
Re: Grep (WAS RE: searching an array)
Message ID:
20020330154328.68452.qmail@onion.perl.org
Hi All,
I was trying to poplulate an array with 6 non-zero unique random numbers
from 1 to 35;
I thought this search a list for a number is so much being used in various
tasks that there would exist some such function
search ( $myvar, @myarr )
which would return true if $myvar existed in the list @myarr.
I tried a lot with grep; but did not succeed.
This is the code (not very refined:) which instead did the job.
So search for how to use grep in such cases or some other function which
will serve the purpose is still on.
Thanks all again,
Mahendra
==
# Cash5 Lotto: draw of 6 unique non-zero numbers from 1..35
my $top = 36; # rand gives a rand numb less than top, hence
my @myarr = ();
my $myrand;
while ( @myarr < 6 ) {
$myrand = rand $top;
$myrand %= 35;
next if $myrand =~ 0;
$t = join ' ', @myarr;
next if $t =~ $myrand;
push @myarr, $myrand;
}
print join ' ', @myarr;
==
"Wagner-David" <Wagner-David@vikingfreight.com> wrote in message
news:113A7A0D1F47D511B92E00D0B7E03DAB0187BCCA@pmail02.vikingfreight.com...
> I was just going by their desire to NOT use foreach. Most of what I do, I
am usually reading in the data and doing the search line by line.
>
> As to grep being a hog, \rong person to ask.
>
> Maybe one of the Perl gurus will answer the group.
>
> Sorry.
>
> Wags ;)
>
> -----Original Message-----
> From: Timothy Johnson [mailto:tjohnson@sandisk.com]
> Sent: Friday, March 29, 2002 16:16
> To: Wagner-David; 'Mahendra Thacker'
> Cc: beginners@perl.org
> Subject: Grep (WAS RE: searching an array)
>
>
>
> 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 Previous
|
Thread Next