Gunnar Hjalmarsson wrote:
Richard Lee wrote:
Gunnar Hjalmarsson wrote:
Richard Lee wrote:

------------ code of Gunnar's ----------------

my $numbers_wanted = 2;
my ( @datawanted, @numbers );

LOOP: foreach ( @datas ) {
    my @test = split;
    foreach my $num ( @numbers ) {
        next LOOP if grep( $num->{$_}, @test ) >= $numbers_wanted;
    }
    push @datawanted, $_;
    push @numbers, { map { $_ => 1 } @test };
}

print "$_\n" for @datawanted;

It iterates over @datas and stores some of the elements in @datawanted 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.

@numbers is a help variable where the numbers in previously stored elements are made conveniently accessible for lookups. The expression

    map { $_ => 1 } @test

creates a key/value list where the elements of @test are the keys (see "perldoc -f map"), and

    push @numbers, { map { $_ => 1 } @test };

makes the list an anonymous hash and adds a reference to that hash to @numbers.

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 ( @numbers ) {
       next LOOP if grep( $num->{$_}, @test ) >= $numbers_wanted;
   }

Since @numbers 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, I think there was a misunderstanding.
I was just going over your code so that I can understand.. as I don't have a solution on my own To understand it better, I was debugging the code and I thought it was going over the loop(but second look gave me the realization that it's skipping it)...

I appreciate your code and it's helping me understand the problem and solution.

thank you again.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to