[mplspm]: In a sorted list [better]

Tim Burlowski tim.burlowski at veritas.com
Fri Mar 22 11:19:46 CST 2002


Whoops I think I misread the requirements. Let me try again.

Looking for literal "string".

#!/usr/bin/perl
my @list=(1,2,3,4,1,2,3,4,5,6,"string","a","string");
my %count = ();
my $string = "1";
$count{$_}++ for (@list);
foreach (keys %count) {
    print "$_ occurs $count{$_} in file\n" if /string/;
}

tim


Previously Tim Burlowski(tim.burlowski at veritas.com) wrote: 

> I read my mail in FIFO, so maybe someone has already submitted this
> solution. It's a cookbook entry 5.14 from the Perl Cookbook.
> 
> -------------------------------------------------
> #!/usr/bin/perl
> my @list=(1,2,3,4,1,2,3,4,5,6);
> my %count = ();
> for(@list){
>    $count{$_}++;
> }
> foreach (keys %count) {
>     print "$_ occurs $count{$_} in file\n";
> }
> -------------------------------------------------
> 
> If you like terser lines.
> 
> -------------------------------------------------
> #!/usr/bin/perl
> my @list=(1,2,3,4,1,2,3,4,5,6);
> my %count = ();
> $count{$_}++ for (@list);
> print "$_ occurs $count{$_} in file\n" foreach (keys %count);
> -------------------------------------------------
> 
> tim burlowski
> 
> Previously Josh Aas(josha at mac.com) wrote: 
> 
> > Hey MPM,
> >     If I have an alphabetically sorted array of strings (containing up to 2
> > million strings), and I want to find out if any strings in that array equal
> > a certain string (yes or no, not how many), what is the fastest way to do
> > that search? This seems basic to me, I just can't come up with the answer
> > and I have an hour to do so. Thanks a lot!
> > -Josh
> > 
> > 
> > 
> > --------------------------------------------------
> > Minneapolis Perl Mongers mailing list
> > 
> > To unsubscribe, send mail to majordomo at pm.org
> > with "unsubscribe mpls" in the body of the message.
> 
> -- 
> tim burlowski
> ========================================
> Weather conditions for Roseville, MN.
> Condition are Fair, temperature is 12 degrees
> with winds From the West at 15. Visibility is Unlimited.
> Dewpoint 0, barometer reads 30.34 humidity at 49%.

-- 
tim burlowski
========================================
Weather conditions for Roseville, MN.
Condition are Fair, temperature is 14 degrees
with winds From the West Northwest at 14. Visibility is Unlimited.
Dewpoint 0, barometer reads 30.33 humidity at 46%.


--------------------------------------------------
Minneapolis Perl Mongers mailing list

To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.



More information about the Mpls-pm mailing list