Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Why isn't this simple method to join elements of 2 arrays not working

by Axlex (Initiate)
on Sep 21, 2014 at 14:35 UTC ( [id://1101453]=perlquestion: print w/replies, xml ) Need Help??

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

Hello wise Monks; i am trying to concatenate the lines of 2 files and came up with this simple code
my (@In, $In, @Out, $Out, $InFile, $OutFile); + # declare variables open $InFile, "<", "e:/File1.txt"; # open the exported +file in read mode open $OutFile, ">>", "e:/File2"; @In = <$InFile>; @Out = <$OutFile>; close $InFile; close $OutFile; for my $k ( 0 .. $#In ) { $Out[$k] = "$Out[$k],$In[$k]"; } open $OutFile, ">", "e:/File2.txt"; # open the outfile, + deleting all content (writemode) print $OutFile @Out; close $OutFile;

but i am getting the error: "Use of uninitialized value within @Out in concatenation(.) or string at join.pl"

I know, my programming skills are bad but please, i dont know any other way.

  • Comment on Why isn't this simple method to join elements of 2 arrays not working
  • Download Code

Replies are listed 'Best First'.
Re: Why isn't this simple method to join elements of 2 arrays not working
by ikegami (Patriarch) on Sep 21, 2014 at 14:42 UTC
    You are trying to read from a file you opened for writing.
Re: Why isn't this simple method to join elements of 2 arrays not working
by LanX (Saint) on Sep 21, 2014 at 14:44 UTC
    open $OutFile, ">>", "e:/File2";

    Is wrong, use "<" to read, @Out must be empty otherwise.

    ">>" means appending !

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Why isn't this simple method to join elements of 2 arrays not working
by davido (Cardinal) on Sep 21, 2014 at 18:12 UTC

    Just for fun...

    use strict; use warnings; use autodie; use Getopt::Long; use File::Slurp qw( read_file write_file ); use List::MoreUtils 'pairwise'; use constant { EMPTY => q{}, FILE1 => 'e:/File1.txt', FILE2 => 'e:/File2.txt' }; my $LIVE = EMPTY; GetOptions('live' => \$LIVE); write_file $LIVE ? FILE2 : \*STDOUT, pairwise { join( ',', map{$_//EMPTY} $a,$b ) . "\n"; } @{slurp(FILE1)}, @{slurp(FILE2)}; sub slurp { read_file shift, array_ref=> 1, chomp => 1 }

    By default this will dump its output to STDOUT. Once you're certain it will do what you want, run it with the --live flag, and it will overwrite FILE2, which seems to be what you want. I'd probably use copies of the original files until I'm certain.

    Updated: Handle command-line option via Getopt::Long rather than @ARGV.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101453]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found