itshardtogetone@[EMAIL PROTECTED]
wrote:
> From: "Richard Lee" <rich.japh@[EMAIL PROTECTED]
>
> To: <itshardtogetone@[EMAIL PROTECTED]
>
> Cc: <beginners@[EMAIL PROTECTED]
>
> Sent: Sunday, May 04, 2008 3:17 AM
> Subject: Re: how to simplify this script
>
>> I don't have the solution yet but shouldn't the answer be
>> 1 6 7
>> and
>> 4 5 10 only ?
>> it's printing out 1 2 3 as well which is wrong?
>
> Thanks Richard.
> The output should include '1 2 3'.
> Therefore I want the output to be
> 1 2 3
> 1 6 7
> 4 5 10
>
> 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.
>
> Thanks
>
>
>
>
>> itshardtogetone@[EMAIL PROTECTED]
wrote:
>>> Hi,
>>> In the script below, I have an array @[EMAIL PROTECTED]
with its elements
>>> consisting of numbers like this :-
>>>
>>> my @[EMAIL PROTECTED]
= (
>>> '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9', '1 6 7',
>>> '1 7 12', '2 6 7', '4 5 10', '4 5 15'
>>> );
>>>
>>> Out of the above list, I wish to generate a seperate array so that
>>> among their elements, there should not be any 2 numbers that can
>>> match and so the output should be :- 1 2 3
>>> 1 6 7
>>> 4 5 10
>>>
>>> I therefore have written the script below and have tested the script
>>> to be working. What is the easier way to write this script?
>>> Thanks
>>>
>>> ##### start of script ##############
>>> use strict;
>>> use warnings;
>>>
>>> my @[EMAIL PROTECTED]
= (
>>> '1 2 3', '1 2 4', '1 2 7', '1 2 8', '1 2 9', '1 6 7',
>>> '1 7 12', '2 6 7', '4 5 10', '4 5 15'
>>> );
>>> my $numbers_wanted = 2;
>>>
>>> my @[EMAIL PROTECTED]
= ();
>>> my $marker = scalar @[EMAIL PROTECTED]
>>> my $splice_counter = 0;
>>> my @[EMAIL PROTECTED]
= ();
>>>
>>> while ($marker) {
>>> $splice_counter = 0;
>>> push @[EMAIL PROTECTED]
splice @[EMAIL PROTECTED]
0, 1;
>>> my @[EMAIL PROTECTED]
= split / /, $datawanted[-1];
>>> my $temp_datas_iterator = scalar @[EMAIL PROTECTED]
- 1;
>>>
>>> foreach ( 0 .. $temp_datas_iterator ) {
>>> my $counter = 0;
>>> my @[EMAIL PROTECTED]
= split / /, $datas[ $_ - $splice_counter ];
>>>
>>> foreach (@[EMAIL PROTECTED]
) {
>>> my $a = $_;
>>> foreach (@[EMAIL PROTECTED]
) {
>>> if ( $a == $_ ) {
>>> $counter++;
>>> }
>>> }
>>> }
>>> if ( $counter >= $numbers_wanted ) {
>>> splice @[EMAIL PROTECTED]
$_ - $splice_counter, 1;
>>> $splice_counter++;
>>> }
>>> }
>>> $marker = scalar @[EMAIL PROTECTED]
>>> }
>>>
>>> foreach (@[EMAIL PROTECTED]
) {
>>> print "$_\n";
>>> }
>>>
>>>
>>>
>
>
I still don't understand
" 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.
Also can someone explain to me in detail what Gunnar Hjalmarsson's
solution is doing?
------------ 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]
did the hash reference %{$num} come from? as in if grep(
$num->{$_} ???
I have difficult time understanding this type of algorithm centric
problems.......


|