[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] perl infinite loop? -- array comparison




When I've got to do comparisons of this nature, I use the table construct 
and exists function. 

# mark each file in the table as new or old
foreach $title (@new_titles ) {

     # title already in table, mark as an old
     if ( exists( $allTitles{ $title } )) {
          $allTitles{ $title } = 0;
     }

     # else mark the title as a new entry
     else {
          $allTitles{ $title } = 1;
     }
}

# loop back over the table looking for all new
# titles and add to the 'brand_new_titles' array
foreach $title ( keys( %allTitles )) {
     if ( $allTitles{ $title } ) {
          push( @brand_new_titles, $title );
     }
}

If you need to maintain the same order try, maintain an indexing array 
and use it for accessing the table elements in the 2nd loop instead of 
the 'keys' function. 

          
>From 'xcom2@panix.com' on '11/11/98 8:38 AM'

>I'm trying to write a Perl script to automate the updating of a flat text 
>file database. I'm trying to compare the new database of titles to the 
>old databse of titles and get a list of "Brand New Titles."
>
>Everything goes fine up untill the following chunk of code where the 
>actual comparison is supposed to happen. When the script gets to this 
>point it gets stuck and just spins its wheels. Is there an infinite loop 
>here? Is there a more efficient way to do this?
>
>while (@new_titles)
>{
>	$i = shift (@new_titles);				     # grab the title on the top
>	
>	$j = 0;
>	
>	foreach $k (@old_titles)			       # compare the new title to each of the 
>old titles
>		{
>		if ($i eq $k)					               # untill we find a match
>			{
>			splice (@old_titles, $j, 1);    # remove the matched title from the 
>old titles list	
>			last;							                    # so we don't check it again and stop 
>looking
>			}
>		elsif ($#old_titles < 1)			      # but if we're at the end of the list 
>of old titles
>			{								                       # then we found a brand new title 
>			push (@brand_new_titles, $i);		 # so we push it onto brand_new_titles
>			}
>		else								                     # if we don't find a match 						
>			{	
>			$j++;							                    # increment the counter and keap 
>looking
>			}	
>		}
>}	
>
>
>***** Want to unsubscribe from this list?
>***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch
>


___________________________________________________________________
Paul Steinkamp                                   steinkam@apple.com
Localization & Release Engineering          Cupertino: 408/974-1665
Apple Computers, INC                       Washington: 425/481-8111


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch