develooper Front page | perl.beginners | Postings from May 2008

Re: how to simplify this script

Thread Previous | Thread Next
From:
itshardtogetone
Date:
May 3, 2008 15:44
Subject:
Re: how to simplify this script
From: "Richard Lee" <rich.japh@gmail.com>
To: <itshardtogetone@hotmail.com>
Cc: <beginners@perl.org>
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@hotmail.com wrote:
>> Hi,
>> In the script below, I have an array @datas with its elements consisting 
>> of numbers like this :-
>>
>> my @datas = (
>>     '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 @datas = (
>>     '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 @datawanted     = ();
>> my $marker         = scalar @datas;
>> my $splice_counter = 0;
>> my @datas_iterator = ();
>>
>> while ($marker) {
>>     $splice_counter = 0;
>>     push @datawanted, splice @datas, 0, 1;
>>     my @comparator1         = split / /, $datawanted[-1];
>>     my $temp_datas_iterator = scalar @datas - 1;
>>
>>     foreach ( 0 .. $temp_datas_iterator ) {
>>         my $counter     = 0;
>>         my @comparator2 = split / /, $datas[ $_ - $splice_counter ];
>>
>>         foreach (@comparator2) {
>>             my $a = $_;
>>             foreach (@comparator1) {
>>                 if ( $a == $_ ) {
>>                     $counter++;
>>                 }
>>             }
>>         }
>>         if ( $counter >= $numbers_wanted ) {
>>             splice @datas, $_ - $splice_counter, 1;
>>             $splice_counter++;
>>         }
>>     }
>>     $marker = scalar @datas;
>> }
>>
>> foreach (@datawanted) {
>>     print "$_\n";
>> }
>>
>>
>>


Thread Previous | Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About