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

Re: Grep (WAS RE: searching an array)

Thread Previous | Thread Next
From:
Jenda Krynicky
Date:
March 30, 2002 08:11
Subject:
Re: Grep (WAS RE: searching an array)
Message ID:
3CA5F19F.26844.8E691C@localhost
From: "Mahendra Thacker" <mnt_cal@snet.net>
> 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.

Usualy if you need to ask "can this thing be found in that set of 
values" the proper data structure is a hash, not an array.

If you define the hash like this:

	@greeting{ 'hello', 'hi', 'good morning', 'good night', 
		'good afternoon'} = ();
	# yes this defines a hah %greeting
	# if this looks too unreadable use
	#	 $greeting{hello} = undef;
	#	 $greeting{hi} = undef;

the you can later ask:

	if (exists $greeting{$something}) {
		print "$something is a greeting\n"
	} else {
		print "$something is NOT a greeting\n"
	}

> I tried a lot with grep; but did not succeed.


sub search ($@) { 
	my ($myvar, @myarr) = @_;
	return (scalar(grep {$_ eq $myvar} @myarr) ? 1 : 0);
}


> 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;

You have a bug in there. This would never give you 35. Keep in 
mind that ($x modulo 35) is a number between 0 and 34 (inclusive).

If you want a random number from 1..35 you should do this:

	$randnum = 1 + int(rand 35);

And if you want 6 unique :

	my @myarr;
	{		# this block is here just to restrict the availability 
			# of %seen
		my %seen;
		while (@myarr < 6) {
			my $num = 1 + int(rand 35);
			push @myarr, $num
				unless $seen{$num}++
		}
	}

if you wanted that a bit longer:

	my @myarr;
	{		# this block is here just to restrict the availability 
			# of %seen
		my %seen;
		while (@myarr < 6) {
			my $num = 1 + int(rand 35);
			if (! $seen{$num}) {
				push @myarr, $num;
				$seen{$num} = 1
			}
		}
	}

Jenda

=========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
					--- me

Thread Previous | 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