Autore Topic: Usare WITH con non-Oggetti  (Letto 330 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.316
  • Ne mors quidem nos iunget
    • Mostra profilo
Usare WITH con non-Oggetti
« il: 31 Luglio 2014, 11:22:03 »
Vi riporto questa discussione:


" 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:

Codice: gambas [Seleziona]
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
Codice: gambas [Seleziona]
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:

Codice: gambas [Seleziona]
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

Codice: gambas [Seleziona]
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:

Codice: gambas [Seleziona]
WITH <target>
  .something .... .something .....
END WITH

to:
Codice: gambas [Seleziona]
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
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »