
Subbeh
New User
Aug 2, 2012, 2:17 AM
Post #1 of 2
(628 views)
|
|
errors with grep
|
Can't Post
|
|
Hi, I just started learning perl, and as an exercise I decided to create a program to find all words in a file that contain certain letters. If I run it like "words.pl -c estt", and the 'dictionary' file contains the word 'test', it should return this word. Instead, I get this a bunch of times: Use of uninitialized value in string eq at ./words.pl line 23, <FILE> line 1. The is what I made so far:
#!/usr/bin/perl use strict; use warnings; use Getopt::Std; use vars qw/ %opt /; getopts('c:', \%opt); my $chars = $opt{c}; my $dict = "dict.txt"; open FILE, $dict; TEST: while (my $word = <FILE>) { chomp $word; if ($word =~ /^[$chars]*$/) { my @characters = split(//,$chars); for (my $i=0;$i<length($word);$i++) { my $char = substr($word,$i,1); my @index = grep { $characters[$_] eq $char } 0 .. $#characters; if ($index[0]) { delete $characters[$index[0]]; } else { next TEST; } } print "found: $word\n"; } } I think it has to do with the grep function, but I'm not sure how to get this to behave normally. Please help me in the right direction, and if you see anything that could be better, please tell me as well. Thanks! :)
|