Tor Hildrum wrote:
> 
> I have 2 questions.
> 
> Here is the code:
> #!/usr/bin/perl
> use warnings;
> use strict;

Very good start.


> # Merging two files
> 
> print ("Write the two files you wan't to merge and \n");
> print ("lastly the file to merge them too, seperated by a space.\n");

Parentheses are usually used for precedence and aren't really required
here.


> chop (my $filenames = <STDIN>);

You should be using chomp() instead of chop() and the parenthese _are_
required here.

chomp( my $filenames = <STDIN> );


> my @names = split(/ /,$filenames); # Get the filenames in an array.

You can always assign a list to a list of scalars.  Also, what happens
if you have file names with spaces?

my ( $firstfile, $secondfile, $writefile ) = split / /, $filenames;


> open (FIRSTFILE, "$names[0]") || die ("Couldn't open $names[0]'n");
> my @firstfile = <FIRSTFILE>;
> 
> open (SECONDFILE, "$names[1]") || die ("Couldn't open $names[1]\n");
> my @secondfile = <SECONDFILE>;
> 
> open (WRITEFILE, ">$names[2]") || die ("Couldn't open $names[2] for
> writing\n");

You should include the $! variable in your error messages.


> # Now to merge the 2 files.
> 
> my $counting = 0;
> 
> while ($firstfile[$counting] ne "" || $secondfile[$counting] ne "" ) {
>         print WRITEFILE ($firstfile[$counting]);
>         print WRITEFILE ($secondfile[$counting]);
>         $counting++
> }

while ( @firstfile or @secondfile ) {
    print WRITEFILE @firstfile  ? shift @firstfile  : '',
                    @secondfile ? shift @secondfile : '';
    }


> The first thing I don't understand. How come this works even if there are
> blank lines in a file, with text after that line?

Blank lines are not equal to "" unless you chomp the lines and remove
the newlines.

> Shouldn't the while-loop end at blank lines?

No.

> I have tested it on two files and it works on the whole
> file, even though there are blank lines there. Why is this?

Do you _want_ it to stop at the fisrt blank line?


> PS: The first file is 1 line, the second is 5 lines with line nr 4 beeing a
> blank line. Line 5 still shows though.
> PS2: I'm aware that my decision to use $counting is dumb, and that I instead
> should use the length of the longest array($firstfile or $secondfile), or
> isn't that a better solution?
> 
> My second question is:
> When using use warnings;
> I get a long list of messages(warnings) when running this script.
> Use of uninitialized value in string ne at ./26.pl line 23, <SECONDFILE>
> line 6.
> Use of uninitialized value in print at ./26.pl line 24, <SECONDFILE> line 6.
> I get this pair 6 times in a row, when the script is run.
> Is this a problem, or is it normal?

It's not normal.  If @secondfile has only one line stored in
$secondfile[0] then trying to compare or print from $secondfile[1] or
$secondfile[2] etc. will cause this warning.


> PS: I'm just starting out, so don't flame me if the code is poorly
> written(feel free to comment though) :)

It's a lot better than some of the stuff I've seen posted to usenet.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to