On 3/14/09 Sat Mar 14, 2009 5:28 AM, "M. Coiffure" <coiff...@gmx.at> scribbled:
> Hi all > > I'm getting this error on the following (test) script: > > Can't call method "x" without a package or object reference at test.pl line 12 > <ENT> line 1 > > What I want to do is create a HashMap where the keys are names of accented > characters (as they are used in entities) and the values the UTF character > itself. > > What am I doing wrong? > > ENT has lines that look like this: > aacute 00E1 > > > here's my script (test.pl): > > > --- > > #!/usr/bin/perl > use strict; > use warnings; > > my %ent; > open ENT, "entities.txt" or die "cannot read entities: $!"; > > while (<ENT>) { > chomp; > m/^(.+) (.+)$/; > print "[$1]\t[$2]\n"; > $ent{$1} = \x{$2}; > } > > > --- > > before the error, the script prints (as expected): "[aacute]\t[00E1]\n" > > Is there anything very fundamental I'm overlooking? (please be patient with > me, I'm not a computer scientist, but a linguist doing some basic > programming.) You are using captured values ($1, $2) from a regulare expression match without first checking if the match succeeded: if( m/^(.+) (.+)$/ ) { print "[$1]\t[$2]"; }else{ print "No match for $_\n"; } Without seeing all of your data, it is impossible to tell what actually went wrong. Possibly the next line after 'aacute 00E1' did not match, perhaps because it was blank or contained something other than one space between entries. I think I would use split instead of a regular expression to parse lines of that type (untested): while(<ENT>) { chomp; my @fields = split; if( @fields == 2 ) { $ent{$fields[0]} = $fields[1]; }else{ print "Invalid line: <$_>; } } At the least I would make the regular expression something more flexible with regard to whitespace: m/^(\S+)\s+(\S+)$/ or perhaps the more restrictive m/^(\w+)\s+(\w+)$/, depending upon the characters actually allowed in your data file. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/