Bob Williams wrote:
Hi,

Hello,

I am trying to split the lines in a file into two halves (at the first space) each half going into an array. The code I have written is below. The print command is there to test that things worked correctly, but it only gives an error for each instance of the print command...

Use of uninitialized value within @uktrackname in print at
        /home/bob/Documents/scripts/perl/music_md5_compare.pl line 22 (#1)

---Code---
#!/usr/bin/perl
use warnings;
#use strict;

You shouldn't comment out that line. Strictures can help you find mistakes in your code.

#use diagnostics;

# perl script to compare two files of md5 checksums
#+and extract the lines which differ

open (BHF_FILE, "</home/bob/tmp/md5music")
        or die "Could not open md5music: $!";
@bhffile = <BHF_FILE>;

Why are you reading the entire file into memory? You only need to process one line at a time.

close (BHF_FILE);

for (my $i = 0; $i <= $#bhffile; $i++){

That is usually written as:

for my $i ( 0 .. $#bhffile ) {

        $bhffile[$i] =~ m/(\s.+) ( .+)/;

What exactly does your data look like? Your regular expression says: match a single WHITESPACE character followed by one or more of ANY character followed by a single SPACE character followed by another single SPACE character followed by one or more of ANY character.

        push @ukchecksum, $1[$i];
        push @uktrackname, $2[$i];

You do not define the arrays @1 or @2 anywhere so why are you trying to use them here? If you want the contents of the capturing parentheses from the regular expression then they are in the variables $1 and $2 but you should only use them if you verify that the regular expression matched successfully. You would probably be better off using an array of arrays or a hash instead of two separate arrays.

}


print @ukchecksum[0];           # Won't print - uninitialized value ???
print @uktrackname[0];  # Won't print - uninitialized value !!!

Why are you using an array slice to access a scalar value? Perl should have warned you about that.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to