Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Longest repeated string...

by redEvo (Novice)
on Jun 13, 2011 at 17:09 UTC ( [id://909411]=note: print w/replies, xml ) Need Help??


in reply to Longest repeated string...

I know this thread is pretty old, but I just read it and saw a way to improve the code. One thing you could do that should speed things up is to store the results in a hash. You can then avoid using uniq and you will only be doing the matching operation on pieces you haven't yet seen.
#!/usr/bin/perl -w use strict; my $line; my %count; while ( defined ( $line = <> ) ) { chomp $line; for my $a ( 1 .. length ( $line ) ) { for my $b ( 1 .. ( length ( $line ) + 1 - $a ) ) { my $out = substr ( $line, $a - 1, $b ); next unless $count{ $out }; # so you don't have to use uniq next if ( $count{ $out } = () = $line =~ /$out/g ) > 1; print "length: $b digits: $out quantity: $count{ $out }"; } } }
I just glanced through the other comments and saw some good points in there too, so if you're using this code make sure to take those into account as well. I didn't want to rewrite the whole thing, but just wanted to point out how the hash could be useful.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found