in reply to match a word in an words' array

Although, unfortunately, I don't quite understand what the problem at hand is or what exactly it is that you are asking, try this code:
sub addfor{ my ($mainwidget,$entryVariable) = @_; my $ex = 0; if (grep(/^$entryVariable$/,@history)) { $mainwidget->messageBox('-icon' => 'error', -type => 'OK', -t +itle => 'Error for input',-message=>"duplicate input"); $ex= 1; } unless ($ex){ $widget{'forwardlistbox'}->insert('end', $entryVariable); push (@history, $entryVariable); } }
Here, I got rid of the foreach() loop and replaced it (and consequent if statement) with a single grep. Also, your original loop would make a message box (annoying pop up i'd guess) appear every time the entry variable is found in the history array. This is no good since also the error message is staying the same all the time.

Also, you didn't have to use strings for defining true/false state. Just use a simple 1 or 0.

UPDATE: changed the grep line. Was passing scalar instead of an EXPR. However, this was done long before you've made your reply, samtregar. In fact as soon as I hit the 'Submit' button the first time and spotted the same slip. And excuse me, but that's a pretty rough language you chose to use there buddy!

UPDATE: Thanks samtregar, it really wasn't my intent to make an 'ass' out of you nor anyone else on PM. I have too much respect for fellow monks to do anything harmful.

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Replies are listed 'Best First'.
Re: Re: match a word in an words' array
by samtregar (Abbot) on May 02, 2002 at 16:52 UTC
    Hey, watch your code man! This doesn't work:
    if (grep($entryVariable,@history)) {
    You can't just pass a scalar as the first arg to grep() and expect it to DWYM.

    -sam

    UPDATE: You just changed it without an update comment. Are you trying to make me look like an ass? Don't bother, I've got that covered.

    UPDATE, THE REVENGE: You have my apologies. Just trying to protect the innocent from bad code...