[Gambas-user] Using WITH on non-objects

B Bruen bbruen at ...2308...
Thu Jul 31 05:17:19 CEST 2014


TRY:
Often I find that I have to do multiple actions on a non-object variable, for instance I might be working with a large string where I need to replace multiple sub-strings, sometimes on a conditional basis. For example, suppose I have a string  S which is a pattern and contains something like "aaa bbb ccc eee".  (Note1 : this is NOT a Subst pattern string!)

Currently I need to write a lot of code like:
  wkS=IIf(somecondition, Replace$(S,"aaa","Somebody"),"")
  wks=IIf(someothercondition, Replace$(wkS,"bbb","something","else")
  ...
  If instr(wkS,"ddd")>0 then Replace$(wkS,"ddd","Tuesday fortnight)
  ...
  etc

Would it be possible to provide within gambas a "shortcut" method for such whereby I could write
 wkS=S
 With wkS
    IIf(somecondition, Replace$("aaa","Somebody","")
   ...
   etc
End With

where wkS is a String variable, i.e. not an object.

It's just an idea, I was wondering if it is possible.  I have written quite a lot of sed calls to achieve the things I need to do but they are a royal PITA to develop in the first place and an extremely royal PITA to maintain.

CATCH:
Another example might illuminate further.  Again I'll talk about string variables as they are easiest to convey the idea.
Since 3.5 I can do the following sorts of things:
  Dim MyWeather As String ="Tuesday -32C"
  Dim MyWeatherComment As String[] = Scan(MyWeather, "* *C")

  With MyWeatherComment
    .[0]=Replace( .[0], "Tuesday", "Tomorrow the maximum temp will be" )
    .[1]=IIf ( Val ( .[1] ) < 19, .[1] & " Brrrrr!", .[1] & " Phew!")
    etc
  End With
  Print MySArray.Join(" ")

which results in "Tomorrow the maximum temp will be -32 Brrrrr!"

What I would like to do is 
  Dim MyWeather As String = "Tuesday 1st August 2014. Exp Max:-32C Hum:2%"
  Dim MyComment As String="Today the maximum temp will be &1 &2"
  With Scan(MyWeather,"* *. Exp Max:*C *)[2]
    Print Subst(MyComment, ., IIf( Val(.)<19,"Brrr!", "Phew")
  End With
    
FINALLY:
In the above pretend code of mine it might be hard to see but on that Print line I'm just using dot, (.) to represent the current With target.  I don't know how the compiler works with WITH but given the above array constructs we now have, it looks like it expands:
WITH <target>
  .something .... .something .....
END WITH
to:
WITH <target>
  <target>.something .... <target>.something .....
END WITH

So what I am asking for is an extension such that "." (i.e. " . ") means the current WITH target in any context, or in other words it refers to the WITH target regardless of whether that target is an object or a native datatype. So a lone . would refer to the WITH target itself.

regards
Bruce
-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list