Gunnar Hjalmarsson wrote:
Richard Lee wrote:
" It should include '1 2 3' because '1 2 3', '1 2 4', '1 2 7', '1 2 8',
'1 2 9' = '1 2'(the common number from the list) + anynumber. "
as any of them contains 1 and 2 and I don't understand why '1 2 3' was
picked.

My interpretation: Because it's the first element of those with 1 and 2 in them. In other words, the order in which the elements appear in @datas is important for the result.

Also can someone explain to me in detail what Gunnar Hjalmarsson's
solution is doing?

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



perl -d ./iii

Loading DB routines from perl5db.pl version 1.25
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(./iii:9):        my @datas = (
main::(./iii:10): '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9', '1 6 7',
main::(./iii:11):           '1 7 12', '2 6 7', '4 5 10', '4 5 15'
main::(./iii:12):       );
 DB<1> s
main::(./iii:14):         my $numbers_wanted = 2;
 DB<1> x @datas
0  '1 2 3'
1  '1 2 4'
2  '1 2 7'
3  '1 2 8'
4  '1 2 9'
5  '1 6 7'
6  '1 7 12'
7  '2 6 7'
8  '4 5 10'
9  '4 5 15'
 DB<2> s
main::(./iii:15):         my ( @datawanted, @numbers );
 DB<2>
main::(./iii:17):         LOOP: foreach ( @datas ) {
 DB<2> x $_
0  undef
 DB<3> s
main::(./iii:18):             my @test = split;
 DB<3> x $x-^H^H
Unrecognized character \x08 at (eval 8)[/usr/perl5/5.8.4/lib/perl5db.pl:619] line 2. eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D = $^D | $DB::db_stop;
 $;-

;' called at /usr/perl5/5.8.4/lib/perl5db.pl line 619
       DB::eval called at /usr/perl5/5.8.4/lib/perl5db.pl line 3349
       DB::DB called at ./iii line 18
 DB<4> x $_
0  '1 2 3'
 DB<5> x @test
 empty array
 DB<6> s
main::(./iii:19):             foreach my $num ( @numbers ) {
 DB<6> x @test
0  1
1  2
2  3
 DB<7> x @numbers
 empty array
 DB<8> s
main::(./iii:22):             push @datawanted, $_;
 DB<8> x $num
0  undef
 DB<9> s
main::(./iii:23):             push @numbers, { map { $_ => 1 } @test };
 DB<9> x @numbers
 empty array
 DB<10> x @test
0  1
1  2
2  3
 DB<11> x @_
 empty array
 DB<12> x $_
0  '1 2 3'
 DB<13> s
main::(./iii:23):             push @numbers, { map { $_ => 1 } @test };
 DB<13> x $_
0  1
 DB<14> s
main::(./iii:23):             push @numbers, { map { $_ => 1 } @test };
 DB<14> x $_
0  2
 DB<15> x $_
0  2
 DB<16> s
main::(./iii:23):             push @numbers, { map { $_ => 1 } @test };
 DB<16> x $_
0  3
 DB<17> s
main::(./iii:18):             my @test = split;
 DB<17> s
main::(./iii:19):             foreach my $num ( @numbers ) {
 DB<17> x @numbers
0  HASH(0xc0b04)
  1 => 1
  2 => 1
  3 => 1
 DB<18>

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


Reply via email to