Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

foreach problem?

by fastkeys (Novice)
on Mar 05, 2001 at 17:52 UTC ( [id://62229]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there, me again!

I just wrote this:
my @words = split(/ /, $_[0]); my $currentword; my $newquery = "SELECT \"taskID\" from \"repair\" WHERE (\"repairKEYWO +RD1\" = \'\')"; foreach ($currentword, @words) { $currentword =~ /^([\w]+)$/; $newquery = $newquery . " or (\"repairKEYWORD1\" = \'" . $curr +entword . "\')"; $newquery = $newquery . " or (\"repairKEYWORD2\" = \'" . $curr +entword . "\')"; $newquery = $newquery . " or (\"repairKEYWORD3\" = \'" . $curr +entword . "\')"; }

but the output from it is unexpected. Say I call it with "no display" I get the query output but where $currentword should be substituted, I get ''

Thanks

Alex

Replies are listed 'Best First'.
Re: foreach problem?
by boo_radley (Parson) on Mar 05, 2001 at 18:25 UTC
    What's going haywire :
    foreach ($currentword, @words)
    Why's it giving unexpected results :
    because you have what you'd like to be your control variable, $currentword in the parens, which denote the list foreach will loop through. This means that your control variable is really becoming the first element of the array, and $_ steps into the place you think $currentword is taking. Since $currentword is empty when the foreach starts, you (naturally?) get "" as the first element. If you'd like to test this out, change my $currentword to my $currentword="First Element!" and see what happens.

    What to do:
    foreach $currentword ( @words) { ... ... ... }
    This restores $currentword to its rightful throne as control variable.
    actually, if you're only using $currentword in this loop, I'd recommend declaring in the loop :
    foreach my $currentword ( @words)
Re: foreach problem?
by Tyke (Pilgrim) on Mar 05, 2001 at 18:06 UTC
    my @words = split(/\s+/, $_[0]); # <-- Better to split on any whi +tespace my $newquery = qq[SELECT "taskID" from "repair" WHERE ("repairKEYWOR +D1" = '')]; foreach my $currentword (@words) { # <-- Current word goes outsid +e brackets $currentword =~ /^([\w]+)$/; $newquery .= qq[ or ("repairKEYWORD1" = '$currentword')]; $newquery .= qq[ or ("repairKEYWORD2" = '$currentword')]; $newquery .= qq[ or ("repairKEYWORD3" = '$currentword')]; }
    Note use of qq rather than quoted strings

    Does it work any better?

    Update Splits better on /s than /S... but you saw that

      Thanks - just tried that but I still get the same output.

      I've checked that the data I'm sending gets to $_[0] but the output is the same.

      Anybody else?

      Thanks again

      Alex
Re: foreach problem?
by Masem (Monsignor) on Mar 05, 2001 at 19:13 UTC
    Maybe part of the problem, maybe not, but:
    $currentword =~ /^([\w]+)$/;
    in your code as you have it does nothing: since you are only looking for a match, the =~ operator doesn't modifiy $currentword, and you don't use the result of the search ($1) in your subsequent calls. In fact, I can't see what the point of this regex is -- Obviously you're looking for the word part of $current word, but you seem to be implying that you only want the word in that regex with nothing else. Maybe you need an if statement on that regex, and then use $1 in the concatenations.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://62229]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-23 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found