Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Is there a way to compare strings without using an array?

by CountZero (Bishop)
on Oct 18, 2011 at 20:23 UTC ( #932238=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Is there a way to compare strings without using an array?
in thread Is there a way to compare strings without using an array?

Wait ... you mean your proteins are not single letters? But actually a 9 digit number like 648040620? Then the regex solution as mentioned above will not work and I think you better put your data in arrays and use List::Compare to calculate the set intersection and such.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


Comment on Re^3: Is there a way to compare strings without using an array?
Download Code
Re^4: Is there a way to compare strings without using an array?
by Jeri (Scribe) on Oct 18, 2011 at 20:32 UTC
    I like the List::Compare. It looks good. I'm just worried about, because my array will be at least 5 million in length. Can you offer me any peace of mind?

      Abstract your comparison code out into a subroutine and give it a try. Should be quick simple code to write and you'll know exactly how well it performs. Will make changing the code or comparing it to other possible solutions simple as well.

      Here is some code which does what you want:
      use Modern::Perl; use List::Compare; # Make two arrays with fake data my @left = map {'AA' . int(rand (5000000) + 1000000)} (1 ... 5000000) +; my @right = map {'AA' . int(rand (5000000) + 1000000)} (1 ... 5000000) +; my $start = time; my $lc = List::Compare->new('-u', \@left, \@right); { open my $OUT_INTER, '>', './intersection.txt'; say $OUT_INTER $_ for $lc->get_intersection; } { open my $OUT_DIFF, '>', './difference.txt'; say $OUT_DIFF $_ for $lc->get_symmetric_difference; } my $duration = time - $start; say "Duration: $duration seconds.";
      However, on my small laptop (only 2 GByte RAM and running a lot of other programs and services) I get an "Out of memory" error, so two lists of 5 million proteins is too big for it. YMMV if you have a more performant machine. It runs fine and fast for 500,000 proteins (15 seconds), 1,000,000 proteins (52 seconds) and 2,000,000 proteins (185 seconds).

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^4: Is there a way to compare strings without using an array?
by mrstlee (Beadle) on Oct 19, 2011 at 13:47 UTC

    I'm not 100% sure what you want to do here Jeri. The sample below assumes you have a single string composed of 9 character sequences. When the 'for' has run the keys of %uniq will be the unique 9 character sequences.

    It would take a while and a lot of memory over 5 mill x 9 char sequences!

    my $str = join '',(648040620,637132715,649986572,648040620 ); my $proteins_count = length ($str)/9; my %uniq; do { $uniq{$_}++ unless $uniq{$_} } for unpack "(A9)$proteins_count" , $str; print "@{[keys %uniq]}\n";
    Prints ..
    637132715 648040620 649986572
Re^4: Is there a way to compare strings without using an array?
by Jeri (Scribe) on Oct 20, 2011 at 16:01 UTC

    I'm going to give it a shot today. I'll let you know what happens. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (18)
As of 2014-10-30 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls