in reply to Re: Re: Multiple text files
in thread Multiple text files

He meant show us the code...

My guess, even without the code, is that you are re-opening the same handle. You'll want to use a different handle for your second file. I.e. if your first open is something like open(FOO, "<file.txt") the next open shouldn't reuse FOO.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Re: Multiple text files
by Anonymous Monk on May 27, 2003 at 23:55 UTC
    open(STDIN, "ICD_MEAS.txt") or die "Cannot open file"; @row = <STDIN>; $done = 0; $count = 0; while (@row[$done]) { chop(@row); foreach (@row[$count]) { @column = split(/\,/,@row[$count]); $last_name = @column[0]; $middle_name = @column[1]; $first_name = @column[2]; if ($source_type =~ /H/) { print ("$sat_name;"); print ("$meas_name;"); print ("$first_name;"); } if ($source_type =~ /C/) { open(STDIN1, "ICD_MEAS_child.txt") or die "Cannot open file"; @row = <STDIN1>; $done = 0; $count = 0; while (@row[$done]) { chop(@row); foreach (@row[$count]) { @column = split(/\,/,@row[$count]); $status = @column[0]; $age = @column[1]; $address = @column[4]; print ("$sat_name;"); print ("$meas_name;"); print ("$first_name;"); print ("$status;"); print ("$age;"); print ("$address;"); } } }close STDIN1; count++; } done++ }close stdin;
    I don't know what I'm doing wrong b/c the first line is the H so it prints the first line stuff, the second line is a C so it prints the new file stuff and the first file, but wont go back to the first file to get the next line of data so I could see if it's a H or C. It doesn't loop back.

    2003-05-28 edit ybiC <code> tags

      First, look at the perlmonks Site How To page for instructions on using the <code> tag when posting your code -- it will retain the indentation and make sure that things like square brackets and angle brackets come through as intended.

      Second, consider a different approach. From the data samples you provided, it appears that there is no "key" field that serves to cross-reference the "parent" file with the "child" file. That means you need to be confident about how these two files were created: for each line in the parent file with "C" in the fifth field, exactly one line is written to the "child" file. If this is accurate, then a workable approach could go like this:

      • Open the "child" file, read all its contents into an array, and close that file.
      • Now, open the "parent" file; for each record that contains a "C" in the fifth field, use perl's "shift" function to take the next entry off the array of "child" records, and append this to the current "parent" record.

      You shouldn't have to worry about memory constraints unless the "child" file has hundreds of megabytes of content. The code could end up looking like this:

      use strict; open( IN, "ICD_MEAS_child.txt" ) or die "failed to open child data: $! +\n"; my @children = <IN>; # read them all at once; close IN; open( IN, "ICD_MEAS.txt" ) or die "failed to open parent data: $!\n"; while (<IN>) { if ( /,C,/ ) { my $child = shift @children; s/\n/,$child/; # update: added the essential comma s/,/;/g; # update: added this line, as per orig. post } print; } close IN;

      ok, you've got a LOT of issues there.. I was about to avoid doing some work by rewriting & commenting this, but thought you'd be better off doing it yourself.. perl will show you what's going wrong if you start your script with these two lines..

      #!/usr/bin/perl -w use strict;

      cheers,

      J