Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Count similar characters in a row - only once

by $new_guy (Acolyte)
on Jun 28, 2011 at 13:14 UTC ( #911758=note: print w/ replies, xml ) Need Help??


in reply to Re: Count similar characters in a row - only once
in thread Count similar characters in a row - only once

Hi Anonymous Monk, I have had quite a thought about it and I think the thing I would most likely do is to first reduce complexity by removing all duplicated lines with the same integer infront of them. Is this right. I tried running the script below and then ran the script above -jgg.pl. But the result are not good. Any ideas or suggestions how I could marry the two in a sensible fashion

my $file = 'my_data_file.txt'; my %seen = (); { local @ARGV = ($file); #local $^I = '2.txt'; while(<>){ $seen{$_}++; next if $seen{$_} > 1; print; } }


Comment on Re^2: Count similar characters in a row - only once
Download Code
Re^3: Count similar characters in a row - only once
by Anonymous Monk on Jun 28, 2011 at 14:06 UTC

    Hi Anonymous Monk, I have had quite a thought about it and I think the thing I would most likely do is to first reduce complexity by removing all duplicated lines with the same integer infront of them. Is this right.

    Based on your descriptions it sounds right.

    I tried running the script below and then ran the script above -jgg.pl. But the result are not good.

    What is wrong with the results?

    Any ideas or suggestions how I could marry the two in a sensible fashion

    First get your code to do what you want, then think about marriage

      I have edited the script above as follows (below): it writes out a temporary file that I will use while running my second script. The second script will now take the temporary file which I will delete afterwards. Please critisize as it must be 100% accurate.

      #!/usr/bin/perl use strict; use warnings; my $file = 'my_data_file.txt'; my %seen = (); { local @ARGV = ($file); #remove all previous re-organized files my $remove = "my.txt.tmp"; if (unlink($remove) == 1) { print "Existing \"$remove\" file was removed\n +"; } while(<>){ #now make a file for the ouput my $outputfile = "my.txt.tmp"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n" +; exit; } $seen{$_}++; if($seen{$_} !~/-/g){ next if $seen{$_} > 1; print POS; } } } print "\n\nfinished processing file.\n";

        Please critisize as it must be 100% accurate.

        Does it perform the transformation you wanted correctly?

        If so, here is my criticism

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-27 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls