Bob Williams <n...@spam.barrowhillfarm.org.uk> asked:
> 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.

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

use strict; # unless you know what you're doing.

> #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: $!";

Better:

open( my $bfh_file, '<', '/home/bob/tmp/md5music' ) or die ...

> @bhffile = <BHF_FILE>;
> close (BHF_FILE);
> 
> for (my $i = 0; $i <= $#bhffile; $i++){
>       $bhffile[$i] =~ m/(\s.+) ( .+)/;
>       push @ukchecksum, $1[$i];
>       push @uktrackname, $2[$i];
> }

If your track names are unique, you're much better off using an associative 
array ("hash") for your data.

It also makes comparison with track names and checksums from a second list much 
easier:

my %uktrackchecksum;

while( my $line = <$bhf_file> ){
  my( $track, $checksum ) = split /\s+/, $line;
  $uktrackchecksum{ $track } = $checksum;
}

close( $bhf_file );

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

foreach my $track (keys %uktrackchecksum){
  print "$track => $uktrackchecksum{ $track }\n";
}

HTH,
Thomas

PS: Totally OTTOH, please excuse any typos.

--
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