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

...is the error I am receiving with this in the first and second subroutine:
use strict; # finds the words in $otherfile that are not in $wordfile; my $wordfile = shift; my $otherfile = shift; sub dict_line{ our %dict; my $line = shift; print $line; my @words = split $line; #print"@words"; foreach my $word (@words){ $dict{lc $word}++; #print "$word\n"; } } sub find_missing{ our %dict; my $line = shift; my @words = split $line; foreach my $word (@words){ print "$word\n"; my $word = lc shift; print "* $word\n" if !defined $dict{$word}; } } sub read_file{ my $file=shift; my $subref = shift || \&read_line; open (FILE, $file) || die "couldn't open $file $!"; while (my $line = <FILE>) { $subref->($line); } } read_file $wordfile, \&dict_line; read_file $otherfile, \&find_missing;
The $line variable that is being split prints fine
just before the call to split.

Replies are listed 'Best First'.
Re: uninitialized value in split
by dragonchild (Archbishop) on May 17, 2005 at 02:57 UTC
    What are you splitting $line on? The first argument to split is the delimiter to split on. The second argument is the scalar to split up. If it's unspecified, it defaults to $_, which is undefined in your examples. You probably want my @words = split ' ', $line; - try that out and see if it helps.

    I want to commend you on using strict and warnings. Excellently done!


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
      I thought that after a shift in a subroutine, $_ contains the value of the shift.
      But I did what you said, and now have a different problem which I will look at for awhile.
        According to perldoc -f shift
        shift ARRAY shift Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If t +here are no elements in the array, returns the undefined value. + If ARRAY is omitted, shifts the @_ array within the lexical s +cope of subroutines and formats, and the @ARGV array at file sc +opes or within the lexical scopes established by the "eval ''", "BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs +. See also "unshift", "push", and "pop". "shift" and "unshif +t" do the same thing to the left end of an array that "pop" and +"push" do to the right end.

        No mention of setting $_

        TheStudent
Re: uninitialized value in split
by Skeeve (Parson) on May 17, 2005 at 10:14 UTC
    Just a note about how I do the check which words are not defined. If Ihave both files properly splitted and word-counted in %dict and %file I usually do it like this:
    delete @file{keys %dict};
    what remains in %file are the words not found in %dict.

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print