a) > open (BHF_FILE, "</home/bob/tmp/md5music") The modern way to open a file is to:
1) Use the three argument form of open(). 2) Create a variable for the file handle. open (my $BHF_FILE, '<', '/home/bob/tmp/md4music'); while (my $line = <$BHF_FILE>) { #do something with $line } close $BHF_FILE; b) Take a look at this code: use strict; use warnings; use 5.010; my $line = 'hello world goodbye'; my @pieces = split / /, $line, 2; for (@pieces) { say; } --output:-- hello world goodbye At the command line, you can always type: perldoc -f <a function name> to get more information about a function. For instance: $ perldoc -f split c) To access the first element of an array, you do this: my @data = ('hello', 'world', 'goodbye'); say $data[0]; Compare the second line to the expression: $1[$i]. Just like $data[0] is accessing an element of the array @data, the expression $1[$i] is trying to access an element of the array @1. And unless you defined an array called @1 in your code, that will cause an error. perl does automatically define variables called $1, $2, etc., which are assigned portions of a regex match, however they are not arrays. The $ sign in front of the names 1, 2, etc. tells you that they are variables that store scalar values (= a single value). On 1/30/10, Rob Dixon <rob.di...@gmx.com> wrote: > Hi Bob > > I suggest you forget about regular expressions and use the library > function split() instead. Take a look at the code below. > > HTH, > > Rob > > > > use warnings; > use strict; > > my (@ukchecksum, @uktrackname); > > open my $bhf_file, '<', '/home/bob/tmp/md5music' > or die "Could not open md5music: $!"; > > while (<$bhf_file>) { > > my @fields = split ' ', $_, 2; > > if (@fields < 2) { > print "Invalid data format at line $.\n"; > next; > } > > push @ukchecksum, $fields[0]; > push @uktrackname, $fields[1]; > } > > print $ukchecksum[0]; > print $uktrackname[0]; > > > > Bob Williams wrote: >> Hi, >> >> 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; >> #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>; >> close (BHF_FILE); >> >> for (my $i = 0; $i <= $#bhffile; $i++){ >> $bhffile[$i] =~ m/(\s.+) ( .+)/; >> push @ukchecksum, $1[$i]; >> push @uktrackname, $2[$i]; >> } >> >> >> print @ukchecksum[0]; # Won't print - uninitialized value ??? >> print @uktrackname[0]; # Won't print - uninitialized value !!! >> ---Code--- >> >> What am I doing wrong? >> >> Many thanks, >> >> Bob > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/