very simple file stitching problem

Discussion in 'Perl Misc' started by Brian, Feb 25, 2009.

  1. Brian

    Brian Guest

    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
    Brian, Feb 25, 2009
    #1
    1. Advertising

  2. Brian <> wrote:
    >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ürgen Exner, Feb 25, 2009
    #2
    1. Advertising

  3. Brian wrote:
    > 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
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
    John W. Krahn, Feb 25, 2009
    #3
  4. Brian

    Brian Guest

    Thank you all for your rapid and gracious replies.

    On Feb 25, 4:01 pm, "John W. Krahn" <> wrote:
    > Brian wrote:
    > > 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
    > --
    > Those people who think they know everything are a great
    > annoyance to those of us who do.        -- Isaac Asimov
    Brian, Feb 25, 2009
    #4
  5. In article <>, says...

    >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

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Feb 26, 2009
    #5
  6. (Heinrich Mislik) wrote:
    >In article <>, says...


    >>foreach my $index (0..@data1) {

    >
    >ITYM
    >foreach my $index (0..$#data1) {


    Indeed.

    But why are you stealth CCing me?

    jue
    Jürgen Exner, Feb 26, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    472
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,616
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    419
    jacob navia
    Oct 17, 2004
  4. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,288
  5. Mark
    Replies:
    5
    Views:
    2,612
    omara007
    Jan 6, 2010
Loading...

Share This Page