AwsedreswA has asked for the wisdom of the Perl Monks concerning the following question:

I need help with this pseudo-code, I need to use PERL because another piece of a process uses it too.

## Pseudo code for counting iterations of 1 and 0 as listed on new lin +es in a .txt ##PREPARATION WORK** name input_file input_file = “C:\Video_Forensics\case_number_CV000001\frame3000-12 +034.txt” ## this is where the script should be modified to tailor to a specifi +c case or workstation Create file “iterations1.txt” ## this is the file that will be the new list counting the consecut +ive iterations of 1 create file “iterations0.txt” ## this is the file that will be the new list counting the consecut +ive iterations of 0 create object_1count object_1count=0 ## this will count the number of times that 1 has been read since the + last number was 0 create object_0count object_0count=0 ## this will count the number of times that 0 has been read since the + last number was 1 ##PREPARATION WORK** ##BODY** open read-only file input_file ## Open as read-only to avoid disturbing the integrity of the dat +a set ## This file should have only a 1 or a 0 written once on each lin +e and be a .txt file ## It should not be difficult to convert .csv or .xls to .txt read line number 1 of input_file If value=1, run loop1test. else if value=0, run loop0test. (loop1test) ## this loop initiates the logic chain when the data set starts with +a 1 and will be 0 when it changes set object_1count=( object_1count + 1) ## now the program is counting iterations of 1 run 1start_count_loop (end loop1test) (1start_count_loop) read next line If line value=1, set object_1count=( object_1count + 1) run 1start_count_loop ## The next line was still a 1, so continue counting else if line value=0 write object_1count to new line of file “iterations1.txt” then + save and close set object_1count=0 run loop0test else if line value = NULL (blank or there are no new lines) write object_1count to new line of file “iterations1.txt” +then save and close (end 1start_count_loop) (loop0test) set object_0count=( object_0count + 1) run 0start_count_loop (end loop0test) (0start_count_loop) read next line If line value=0, set object_0count=( object_0count + 1) run 0start_count_loop else if line value = 1 write object_0count to new line of file “iterations0.txt” then + save and close set object_0count=0 run loop1test else if line value = NULL (blank or there are no new lines) write object_0count to new line of file “iterations0.txt” +then save and close (end 0start_count_loop) close input_file ##BODY** end

Thanks for all your help

Replies are listed 'Best First'.
Re: Counting iterations of 1 and 0
by Laurent_R (Canon) on Jan 29, 2014 at 08:28 UTC
    If value=1, run loop1test. else if value=0, run loop0test.
    This is obviously not Perl code, but pseudo-code, so I will not offer to correct the syntax or anything, but I would suggest that you learn how to indent code correctly, so that indentation may be useful:
    If value=1, run loop1test. else if value=0, run loop0test.
    This makes clearer that you run loop1test if the value is 1 and that, else, if the value is 0, you run loop0test.
Re: Counting iterations of 1 and 0
by Kenosis (Priest) on Jan 29, 2014 at 04:01 UTC

    I need help with this pseudo-code...

    What kind of help with your pseudo-code do you need?

Re: Counting iterations of 1 and 0
by Bloodnok (Vicar) on Jan 29, 2014 at 14:33 UTC
    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 :-))

      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 :-))
Re: Counting iterations of 1 and 0
by atcroft (Abbot) on Feb 05, 2014 at 07:18 UTC

    Does this do what you are requesting?

    Hope that helps.

      !Perfect!

      Thanks for everyone's help.

        Here is what R let me do with this script's output.

        http://s848.photobucket.com/user/AwsedreswA/media/evidence_zpsb581c0d5.png.html

        Thanks again atcroft and everyone at PerlMonks