Frage

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.

War es hilfreich?

Lösung

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] } {

Andere Tipps

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top