very simple file stitching problem

B

Brian

it may not be obvious from this novitiate perl code below, but i am
trying to add the lines of one file to the lines of another file to
create a third file. the lines in each file are semantically related,
and will always 'line up.'

use strict;

$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
[outfile] - attaches all values from the list of equal values to ea
line of another list ";

my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $outfile = $ARGV[2];

open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
$outfile\n";

my @data1 = <INFILE1>;
my @data2 = <INFILE2>;
close INFILE1;
close INFILE2;

my $sLine;
my $iLineCount = 0;
foreach $sLine (@data1)
{
my $sAddLine = "";
$iLineCount = $iLineCount + 1;
chomp($sLine);
$sAddLine = @data2[$iLineCount];
$sLine = $sLine . "," . $sAddLine;
printf $sLine;
print OUTPUTFILE $sLine;
}

close OUTPUTFILE;

exit(0);

problem is, i can't seem to address the individual items in the array
formed from the second file, from within a loop designed to process
each item in the first array (formed from the first file).

the error message, while understandable, seems unavoidable:

Use of uninitialized value in concatenation (.) or string at
stitch2files.pl lin
e 28.

any help would be appreciated

thanks
 
J

Jürgen Exner

Brian said:
it may not be obvious from this novitiate perl code below, but i am
trying to add the lines of one file to the lines of another file to
create a third file. the lines in each file are semantically related,
and will always 'line up.'

use strict;

$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
[outfile] - attaches all values from the list of equal values to ea
line of another list ";

my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $outfile = $ARGV[2];

open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
$outfile\n";

my @data1 = <INFILE1>;
my @data2 = <INFILE2>;
close INFILE1;
close INFILE2;


If you really want to slurp in both files then replace your loop with
the three lines below:

chomp @data1;
foreach my $index (0..@data1) {
print OUTPUTFILE "$data1[$index],$data2[$index]";
}


However there is really no need because you can read line by line and
print each line as you read it, too (untested):

while (<INFILE1>){
chomp;
print OUTPUTFILE $_ , ",", scalar(<INFILE2>);
}

Again, this assumes that your statement "the lines in each file [...]
will always 'line up.'"" is true.

jue
 
J

John W. Krahn

Brian said:
it may not be obvious from this novitiate perl code below, but i am
trying to add the lines of one file to the lines of another file to
create a third file. the lines in each file are semantically related,
and will always 'line up.'

use strict;

use warnings;
$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
[outfile] - attaches all values from the list of equal values to ea
line of another list ";

@ARGV == 3 or die "Usage: ...";
my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $outfile = $ARGV[2];

my ( $infile1, $infile2, $outfile ) = @ARGV;
open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
$outfile\n";

You should include the $! variable in the error message(s) so you know
why open failed.
my @data1 = <INFILE1>;
my @data2 = <INFILE2>;
close INFILE1;
close INFILE2;

my $sLine;
my $iLineCount = 0;
foreach $sLine (@data1)

foreach my $sLine (@data1)
{
my $sAddLine = "";
$iLineCount = $iLineCount + 1;
$iLineCount++;

chomp($sLine);
$sAddLine = @data2[$iLineCount];

You have just incremented $iLineCount so it will miss the first element
of @data2 and try to read one element past the last element of @data2.
Also, you are using an array slice when you should be using a scalar,
which perl would have warned you about if you had used the warnings pragma.

$sAddLine = $data2[$iLineCount];
$iLineCount++;

Or simply:

$sAddLine = $data2[$iLineCount++];
$sLine = $sLine . "," . $sAddLine;

$sLine .= ",$sAddLine";
printf $sLine;

You are using printf which treats its first argument as a format string
so if you have any '%' characters in $sLine this will not work. Just
use print:

print $sLine;
print OUTPUTFILE $sLine;
}

close OUTPUTFILE;

exit(0);

problem is, i can't seem to address the individual items in the array
formed from the second file, from within a loop designed to process
each item in the first array (formed from the first file).

the error message, while understandable, seems unavoidable:

Use of uninitialized value in concatenation (.) or string at
stitch2files.pl lin
e 28.


John
 
B

Brian

Thank you all for your rapid and gracious replies.

Brian said:
it may not be obvious from this novitiate perl code below, but i am
trying to add the lines of one file to the lines of another file to
create a third file.  the lines in each file are semantically related,
and will always 'line up.'
use strict;

use warnings;
$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
[outfile] - attaches all values from the list of equal values to ea
line of another list ";

@ARGV == 3 or die "Usage: ...";
my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $outfile = $ARGV[2];

my ( $infile1, $infile2, $outfile ) = @ARGV;
open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
$outfile\n";

You should include the $! variable in the error message(s) so you know
why open failed.
my @data1 = <INFILE1>;
my @data2 = <INFILE2>;
close INFILE1;
close INFILE2;
my $sLine;
my $iLineCount = 0;
foreach $sLine (@data1)

foreach my $sLine (@data1)
        {
           my $sAddLine = "";
           $iLineCount = $iLineCount + 1;

                $iLineCount++;
           chomp($sLine);
           $sAddLine = @data2[$iLineCount];

You have just incremented $iLineCount so it will miss the first element
of @data2 and try to read one element past the last element of @data2.
Also, you are using an array slice when you should be using a scalar,
which perl would have warned you about if you had used the warnings pragma.

                $sAddLine = $data2[$iLineCount];
                $iLineCount++;

Or simply:

                $sAddLine = $data2[$iLineCount++];
           $sLine = $sLine . "," . $sAddLine;

                $sLine .= ",$sAddLine";
           printf $sLine;

You are using printf which treats its first argument as a format string
so if you have any '%' characters in $sLine this will not work.  Just
use print:

                print $sLine;


           print OUTPUTFILE $sLine;
        }
close OUTPUTFILE;

problem is, i can't seem to address the individual items in the array
formed from the second file, from within a loop designed to process
each item in the first array (formed from the first file).
the error message, while understandable, seems unavoidable:
Use of uninitialized value in concatenation (.) or string at
stitch2files.pl lin
e 28.

John
 
H

Heinrich Mislik

If you really want to slurp in both files then replace your loop with
the three lines below:

chomp @data1;
foreach my $index (0..@data1) {

ITYM
foreach my $index (0..$#data1) {
print OUTPUTFILE "$data1[$index],$data2[$index]";
}

Cheers

Heinrich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top