3

in if condition I want to check whether the string contains the pattern, but i'm getting following error. missing operand at _@_ in expression " $extra_url eq _@_*user-new* " (parsing expression " $extra_url eq *user-n...") invoked from within "if { $extra_url eq *user-new* } {

code is as follows :

if { $extra_url eq *user-new* } {
     ds_add rp [list notice "File $root/$extra_url: Not found" $startclicks [clock clicks -milliseconds]]
     ds_add rp [list transformation [list notfound "$root / $extra_url" $val] $startclicks [clock clicks -milliseconds]]

 }

here I'm checking whether the extra_url string contains "user_new". I don't know whether I'm doing it right. please help.

VijayD
  • 826
  • 1
  • 11
  • 33

3 Answers3

3

Your problem is on this line:

if { $extra_url eq *user-new* } {

The issue is that the expression parser treats the first * of *user-new* as a multiply operator, and you've got two binary operators in a row without a value-token in between, which is illegal syntax. Indeed, to the expression parser, you might as well have written:

$extra_url eq * user - new *

OK, that's only after tokenizing, and has other problems after the double operator, namely unrecognised barewords and a trailing binary operator; the parse will fail. (Yes, the expression parser is a pretty traditional parser, not entirely different (albeit simpler than) those used to parse C or Java or …)

I'm guessing you actually want either string equality with a literal token (put the token in {braces} or "double-quotes") or you want to use a command to test for some more complex case, such as a glob match with string match. Thus, pick one of these:

if { $extra_url eq {*user-new*} } {
if { $extra_url eq "*user-new*" } {
if { [string match *user-new* $extra_url] } {
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
1

checking whether the extra_url string contains "user_new":

if {[string match {*user_new*} $extra_url]} { ... }

if {[string first "user_name" $extra_url] != -1} { ... }

switch -glob $extra_url {
    {*user_name*} { ... }
}

if {[regexp "user_name" $extra_url]} { ... }

eq is used strictly for string equality.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I wrote something like this `if {[string match {*user_new*} $extra_url]} { set extra_url "filenotfound" }` it gave error as `wrong # args: no script following "[string match {*user_new*} $extra_url]" argument while executing "if {[string match {*user_new*} $extra_url]}"`. – VijayD Apr 22 '14 at 15:23
  • Did you put the opening brace on a new line? Tcl does not allow that. The `if` command requires at least 2 arguments and (since newline is a command terminator) you only gave it 1. – glenn jackman Apr 22 '14 at 15:35
  • Did the changes, but now facing following error `extra characters after close-brace while executing "if {[string match {*user_new*} $extra_url]}{ set extra_url "filenotfound" continue } ad_try { rp_serve_abstract_file "$root..." ` – VijayD Apr 22 '14 at 16:07
  • Tcl is like the shell in that the basic syntax is `command arg arg ...` -- the individual arguments are separated by spaces. If you smash 2 arguments together without whitespace, then Tcl complains. The complete rules for Tcl syntax can be found [here](http://tcl.tk/man/tcl8.5/TclCmd/Tcl.htm) (only 12 rules). You might also benefit from running through the [Tcl tutorial](http://tcl.tk/man/tcl8.5/tutorial/tcltutorial.html). – glenn jackman Apr 22 '14 at 16:29
  • Thanks for the help. but would you be able to tell me what was wrong with this code or what is the right version of this code? – VijayD Apr 22 '14 at 16:37
  • You need a space between `}{` – glenn jackman Apr 22 '14 at 16:41
0

Assuming you are trying an exact string comparison, complementing on Donal Fellows', from the manual pages (with cuts and adaptations):

The if command evaluates the condition as an expression (in the same way that expr evaluates its argument).

Operands for the expr command may be specified in any of the following ways:

  1. As a string enclosed in double-quotes. [...]
  2. As a string enclosed in braces. [...]

Note that it implicitly excludes bare strings. They must be enclosed.

I am not sure why (maybe precisely to avoid confusion between a * as in a string like yours and valid expr operators) and thus it bites me once in a while too.