CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
errors with grep

 



Subbeh
New User

Aug 2, 2012, 2:17 AM

Post #1 of 2 (923 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:


Code
#!/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! :)


Subbeh
New User

Aug 2, 2012, 5:26 AM

Post #2 of 2 (910 views)
Re: [Subbeh] errors with grep [In reply to] Can't Post

Ok, I figured out myself how to get it to work.

I replaced delete with splice, and I changed

if ($index[0])

with

if (defined($index[0]) and $index[0] ne "")

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives