Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: how to simp...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 14 Topic 11021 of 11401
Post > Topic >>

Re: how to simplify this script

by noreply@[EMAIL PROTECTED] (Gunnar Hjalmarsson) May 6, 2008 at 05:48 PM

Richard Lee wrote:
> Gunnar Hjalmarsson wrote:
>> Richard Lee wrote:
>>>
>>> ------------ code of Gunnar's ----------------
>>>
>>> my $numbers_wanted = 2;
>>> my ( @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 );
>>>
>>> LOOP: foreach ( @[EMAIL PROTECTED]
 ) {
>>>     my @[EMAIL PROTECTED]
 = split;
>>>     foreach my $num ( @[EMAIL PROTECTED]
 ) {
>>>         next LOOP if grep( $num->{$_}, @[EMAIL PROTECTED]
 ) >= $numbers_wanted;
>>>     }
>>>     push @[EMAIL PROTECTED]
 $_;
>>>     push @[EMAIL PROTECTED]
 { map { $_ => 1 } @[EMAIL PROTECTED]
 };
>>> }
>>>
>>> print "$_\n" for @[EMAIL PROTECTED]
>>
>> It iterates over @[EMAIL PROTECTED]
 and stores some of the elements in @[EMAIL PROTECTED]
 
>> based on (my interpretation of) the OP's criteria. There is nothing 
>> mysterious with the code; everything can be looked up in the Perl docs.
>>
>> @[EMAIL PROTECTED]
 is a help variable where the numbers in previously stored 
>> elements are made conveniently accessible for lookups. The expression
>>
>>     map { $_ => 1 } @[EMAIL PROTECTED]
>>
>> creates a key/value list where the elements of @[EMAIL PROTECTED]
 are the keys (see 
>> "perldoc -f map"), and
>>
>>     push @[EMAIL PROTECTED]
 { map { $_ => 1 } @[EMAIL PROTECTED]
 };
>>
>> makes the list an anonymous hash and adds a reference to that hash to 
>> @[EMAIL PROTECTED]
>>
>> grep() is used in scalar context to compare the elements with 
>> previously stored elements and test against the OP's criteria.
>>
>>     perldoc -f grep
> 
> why does program go over empty array at the begining?
> 
>    foreach my $num ( @[EMAIL PROTECTED]
 ) {
>        next LOOP if grep( $num->{$_}, @[EMAIL PROTECTED]
 ) >= $numbers_wanted;
>    }

Since @[EMAIL PROTECTED]
 is empty at the first iteration of the outer loop, the 
program skips the inner loop. So what?

I simply posted some code that gives the desired result. Please feel 
free to criticize the code or post an alternative solution, but I don't 
see the point with asking "why" with respect to a piece of code out of 
context.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 




 14 Posts in Topic:
how to simplify this script
itshardtogetone@[EMAIL PR  2008-05-03 21:05:22 
Re: how to simplify this script
rich.japh@[EMAIL PROTECTE  2008-05-03 15:17:13 
Re: how to simplify this script
itshardtogetone@[EMAIL PR  2008-05-04 06:44:18 
Re: how to simplify this script
rich.japh@[EMAIL PROTECTE  2008-05-05 00:31:29 
Re: how to simplify this script
noreply@[EMAIL PROTECTED]  2008-05-05 11:00:28 
Re: how to simplify this script
rich.japh@[EMAIL PROTECTE  2008-05-06 01:25:23 
Re: how to simplify this script
noreply@[EMAIL PROTECTED]  2008-05-06 17:48:51 
Re: how to simplify this script
rich.japh@[EMAIL PROTECTE  2008-05-06 11:56:00 
Re: how to simplify this script
rich.japh@[EMAIL PROTECTE  2008-05-03 15:58:25 
Re: how to simplify this script
noreply@[EMAIL PROTECTED]  2008-05-03 22:22:56 
Re: how to simplify this script
itshardtogetone@[EMAIL PR  2008-05-04 07:03:21 
Re: how to simplify this script
noreply@[EMAIL PROTECTED]  2008-05-04 01:19:40 
Re: how to simplify this script
chas.owens@[EMAIL PROTECT  2008-05-03 21:45:33 
Re: how to simplify this script
chas.owens@[EMAIL PROTECT  2008-05-03 21:51:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 7:03:32 CDT 2008.