HP3000-L Archives

January 2002, Week 2

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"VANCE,JEFF (HP-Cupertino,ex1)" <[log in to unmask]>
Reply To:
VANCE,JEFF (HP-Cupertino,ex1)
Date:
Thu, 10 Jan 2002 19:10:47 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (104 lines)
> When examining character strings in MPE, how do people get around the
> potential extra quote problem, for example:
>
> ADDR = "L'ESPRIT "TEST" VACANCES"
> SETVAR TEST,STR("!ADDR",11,6)

As mentioned by several good CI script writers, you should never
use EXPLICIT variable referencing (!varname) unless either a) you
are forced to (e.g. echo !varname), or b) you are doing trickier
stuff, eg. setvar var!index value, etc.  Had you simply omitted
the "!addr" in STR() all should work fine.

A more general question is, what if you must explicitly reference
a CI variable and you don't know what its contents are? E.g.

  :input foo, "enter anything:"
   (here the user could enter: "abc'def,h>jk"l<n!op  ,for instance)

  :echo foo=!foo
echo foo="abc'def,h>jk"l<n!op                         ^
Variable name not found in variable table. (CIWARN 8101)
echo foo="abc'def,h>jk"l<n!op
NONEXISTENT PERMANENT FILE  (FSERR 52)
echo foo="abc'def,h>jk"l<n!op
Couldn't open input redirection file, command failed. (CIERR 9427)

So what to do???
  :setvar foo anyparm(!foo)
setvar foo anyparm("abc'def,h>jk"l<n!op)
                                    ^
Variable name not found in variable table. (CIWARN 8101)

That didn't work, how about:
  :setvar foo repl(foo,"!","!!")
  :echo foo=!foo
echo foo="abc'def,h>jk"l<n!op
NONEXISTENT PERMANENT FILE  (FSERR 52)
echo foo="abc'def,h>jk"l<n!op
Couldn't open input redirection file, command failed. (CIERR 9427)

Not right still, try:
  :setvar foo repl(repl(repl(foo,"!","!!"),"<","!<"),">","!>")
  :echo foo=!foo
foo="abc'def,h!>jk"l<n!op

That worked (almost!):  It turns out that the CI does NOT process
IO redirection if the IO redirection metacharacter is within a quoted
string. But you don't know if there are IO metachars inside quoted
strings or not (and this was the impetus for the now gone QUOTE()
function). Here is my final answer, which is ugly, IMO:

  input foo, "enter anything:"
    user enters: "abc'def,h>jk"l<n!op
-------------
  setvar bar repl(foo,"!","!!")
  setvar inQuote1 setvar(inQuote2,false)
  setvar i 0
  while setvar(i,i+1) <= len(bar) do
     if setvar(char,str(bar,i,1)) = '"' then
        if inQuote2 then
           # ignore this other quote char
        elseif inQuote1 then
           # found closing quote
           setvar inQuote1 false
        elseif i < len(bar) and setvar(end1,delimpos(bar,'"',,i+1)) > 0 then
           setvar inQuote1 true
        # else an invalid quoted string, no closing quote
        endif
     elseif char = "'" then
        if inQuote1 then
           # ignore this other quote char
        elseif inQuote2 then
           setvar inQuote2 false
        elseif i < len(bar) and setvar(end2,delimpos(bar,"'",,i+1)) > 0 then
           setvar inQuote2 true
        # else an invalid quoted string, no closing quote
        endif
     elseif char = "<" or char = ">" then
        if (inQuote1 and i < end1) or (inQuote2 and i < end2) then
           # IO redirection meta inside quoted string, leave as is...
        else
           # escape the IO meta char
           setvar bar lft(bar,i-1) + "!" + rht(bar,-i)
           setvar i i+1
        endif
     endif
  endwhile
  showvar foo,bar
  echo bar=!bar


FOO = "abc'def,h>jk"l<n!op
BAR = "abc'def,h>jk"l!<n!!op
bar="abc'def,h>jk"l<n!op

This should work for all imaginable input...


FWIW,
 Jeff Vance, CSY

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2