Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Counting iterations of 1 and 0

by Bloodnok (Vicar)
on Jan 29, 2014 at 14:33 UTC ( [id://1072519]=note: print w/replies, xml ) Need Help??


in reply to Counting iterations of 1 and 0

use warnings; use strict; use autodie; open IN, "<file.txt"; binmode IN; my @content = <IN>; close IN; foreach my $num (keys %rec) { my @count = ("@content" =~ /$num+/sg); open my $fh, ">iterations$num.txt"; print "$_\n" for @count; close $fh; }
Untested, but worth a try nonetheless ??

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Counting iterations of 1 and 0
by AwsedreswA (Initiate) on Feb 01, 2014 at 06:11 UTC

    Sorry about my indention error guys, it made sense to me.

    I tested that code Bloodnok Here is what I made it into..

    use warnings; use strict; use autodie; open IN, "<baseline_pattern.txt"; binmode IN; my @content = <IN>; close IN; foreach my $num (keys %rec) { my @count = ("@content" =~ /$num+/sg); open my $fh, ">iterations$num.txt"; print "$_\n" for @count; close $fh; }

    And here is the error message.

    Global symbol "%rec" requires explicit package name at D:\iterations counter.pl

    Line 10.

      Hmm, methinx I made a bit of a showstopping suggestion ... try - foreach my $num (qw/0 1/) {...

      A user level that continues to overstate my experience :-))

        Well, now at least I am getting a long list of 1's.

        Here is what I am using.

        use warnings; use strict; use autodie; open IN, "<D:\\baseline_pattern.txt"; binmode IN; my @content = <IN>; close IN; foreach my $num (qw/0 1/) { my @count = ("@content" =~ /$num+/sg); open my $fh, ">iterations$num.txt"; print "$_\n" for @count; close $fh; }

        There are files "D:\iterations_1.txt" and "D:\iterations_0.txt" that I created.

        However there was nothing written to these files.

        Should there be a sprintf?

        Well something is working right because I got no errors.

        However, nothing was written to either file.

        This is the code now.

        use warnings; use strict; use autodie; open IN, "<D:\\baseline_pattern.txt"; binmode IN; my @content = <IN>; close IN; foreach my $num (qw/0 1/) { my @count = ("@content" =~ /$num+/sg); open my $fh, ">iterations_$num.txt"; print $fh "$_\n" for @count; close $fh; }

        Should the '>' in front of iterations be a '>>' for appending?

        How can I fully qualify where the files are that data should be written?

        I can barely make sense of what is going on in this script

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found