I have a short perl script which I'd like to open another file for reading in data, then create a new file for each unique identifier (caseid). My script currently does this adequately. However, I can't get the $counter variable to reset with each new file.

I suspect there might be a better way to do this, perhaps a hash or arrays, or a hash of hashes. I'm still trying to figure out the best method, but will settle for something that works.

Sample input file (from test.txt)

CASE ID,ITEM,ENTITY,TYPE,ORDER,TEXT

10001,comments@txt,2,os,1,Monday

10001,comments@txt,7,os,1,Tuesday

10001,comments@txt,8,os,1,Wednesday

10001,comments@txt,10,os,1,

Code is as follows
#!/usr/bin/perl use strict; use warnings; use Text::CSV; use Text::Wrap; $Text::Wrap::columns=100; my $file = "test.txt"; my @line_list; #list of csid on each line, contains duplicates my @csid_list; #list of unique csids #my $oldcsid = 0; my $csv = Text::CSV->new or die "Cannot use CSV: ".Text::CSV->error_d +iag (); open (my $fh, '<', "$file") or die "Cannot open $file: $!"; $csv->getline ($fh); #skip header row while ( my $row = $csv->getline( $fh ) ) { push @line_list, $row->[0]; } @csid_list = sort {$a <=> $b} uniq(@line_list); # Adds title to each file foreach my $csid (@csid_list) { open (my $csid_fh, '>>', "$csid.txt"); print $csid_fh "####################\n\nCalYouth Case ID: $csid\n\n# +###################"; close $csid_fh; } close $fh; my $csv2 = Text::CSV->new or die "Cannot use CSV: ".Text::CSV->error_d +iag (); open (my $fh2, '<', "$file") or die "Cannot open $file: $!"; $csv2->getline ($fh2); # Prints notes to correct csid file my $counter = 1; #counts iterations through loop while ( my $row = $csv2->getline($fh2)) { foreach my $csid (@csid_list) { if ($row->[0] == $csid) { if ($row->[5]) { my $temp_string = wrap("\t","\t","$row->[5]"); #wrap() is from Te +xt::Wrap open (my $csid_fh, '>>', "$csid.txt"); print $csid_fh "\n\n\t------------------------------\n\nNote Entr +y $counter:\n\n$temp_string\n\n"; #$counter should count each note in + a file ++$counter; #increments $counter close $csid_fh; } else { next; } } } } # Sorts out duplicates from an array sub uniq { my %seen; grep !$seen{$_}++, @_; } #More experimental code. Sigh. #Reset the counter for new files #if (-e "$csid.txt"){ # $counter++; # print "$csid.txt exists $counter \n"; + #}else{ # $counter=0; #} #Experimental code to handle counter resets. Isn't working. #if ($oldcsid == 0){ # $oldcsid = $csid; # $counter++; #} #elsif ($oldcsid == $csid) { # $counter++; #} #else{ #$counter = 1; Endif for ($row->[0] == $csid). I think th +is will never be false, except on a blank line, which an output file +probably won't have, unless data export >1000 chars. However, removin +g this check broke stuff. #$oldcsid = $csid; #}

In reply to Unable to get counter to reset by morcadiss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.