Game Development Community

String Manipulation in TGE

by Christopher Dapo · in Torque Game Engine · 03/03/2007 (2:37 pm) · 30 replies

EDIT: -!!New Problem Listed Below!!-

I've searched around for information on manipulating strings in Torque and I still have a problem.

My problem:
I need to figure out how to 'read' characters from strings so they can be managed.

So far:
The closest I've come to figuring this out is from TDN:
- TS Console Functions 3 - strlen(string //used to determine string length of chars

...The question is, what other syntax can I use to retrieve characters from the string

Example:
myString = "abcdefg012345"
for(curChar = 0, curChar < strlen(myString), curChar++)
{
getNextChar //how do I?

Any help is appreciated, thank you!
Page«First 1 2 Next»
#21
03/21/2007 (4:54 pm)
There were two errors...

One was the double-quotes.

The other was the backslash!

Backslash required an extra backslash (\escape) in order to register. Check!

This could be done with the double-quotes if..
..anyone knows the escape? =-D

Or is there one?!

Would be very helpful at this point! Thanks to everyone so far!
- Ronixus

Also: All of the remaining chars seem fine so far! Yay!!


EDIT: The escape worked for the double-quotes as well! Sweet! Now, time to take the next step - breakdown and reconstruct the data!

Thanks all!

Note - Might be a good idea to referance these escape characters on TDN (didn't see them there yet). \\ and \" for use in your strings to represent \ and ", respectively.

I'll keep you all posted.
- Ronixus
#22
03/23/2007 (1:48 am)
Taking a break from managing mangled character data, I think I've figured out fileI/O, so I can test it later! It looks promising so far...

I've gone through some of the hard code recently and it is pretty sweet! I'm going to do some good thurough documentation on my own and maybe figure out some tweaks in the future.

All for now...
- Ronixus

Note: Yes, I'm quite fresh to it all...but I'm not.

Hence the name Ronixus ;)
"Thou knoweth what thou dost not knoweth."
#23
03/24/2007 (1:23 am)
Almost at my half way point!!!

Need some help with this one - What's wrong here?
%Q=%rcV;
                if(openForAppend(~/rcTemp.cs))    //open file for write???
                {                                                    //note:TDN states it returns bool but no...
                  writeLine(%Q);   //write to file???    ...example of the correct syntax for any I/O
                }
                close();    //close file???

I take it I'm missing some syntax on the path...

Thanks in advance!
- Ronixus
#24
03/24/2007 (12:33 pm)
After some searching both here and on TDN, I'm still quite stuck on this one. Anyone know much about fileI/O that could lend a helping hand on this?
#25
03/24/2007 (4:51 pm)
OpenForAppend and writeLine aren't functions. They are methods for an object. Where is your fileio object?
#26
03/25/2007 (10:57 pm)
Ok, here's what I've got so far, I know it's probably not too neat nor utilizing of resources and such, but it's my rough draft and its in some form of working order :P

// Start my code!

function rcFilio(%job, %csString)
{
  %file = new FileObject();
  switch(%job)
  {
    case 1:
      %file.openForAppend("~/rcTemp.cs");
      %file.writeLine(%csString);
      %file.close();
    case 2:
      %file.openForWrite("~/rcTemp.cs");
      %file.writeLine("Bye!");
      %file.close();
      %file.delete();
    case 3:
      %file.openForRead("~/rcTemp.cs");
      while( !%file.isEOF() )
      {
        %line = %file.readLine();
        $rcReading = ($rcReading+%line);
      }
      %file.close();
      return($rcReading);
    default:
      echo("Defaulted?!  This can't happen!");
  }
}

function rcSearch(%COn)
{
  for($Dset=1; $Dset<$sets; $Dset++)
  {
    echo("Current Search Is - " @ %COn NL "Current Match - " @ $rcDone[$Dset]);
    if(%COn=$rcDone[$Dset])
    {
      echo("1/4 Match");
      if($rcDtwo[$Dset]=getSubStr(%rcCtwo,%p,1))
      {
        echo("1/2 Match");
        if($rcDtri[$Dset]=getSubStr(%rcCtri,%p,1))
        {
          echo("3/4 Match");
          if($rcDfor[$Dset]=getSubStr(%rcCfor,%p,1))
          {
            echo("Full Match - " @ $rcDa[$Dset]);
            %dOn=$rcDa[$Dset];
            %rcFound=1;
            rcFilio(1,%dOn);
          } else
          {
            echo("No Match on 4");
          }
        } else
        {
          echo("No Match on 3");
        }
      } else
      {
        echo("No Match on 2");
      }
    } else
    {
      echo("No Match on 1");
    }
  }
  if(%rcFound != 1)
  {
    %rcFound = 0;
  }
  return(%rcFound);
}

function rcDcode(%rcstring)
{
  //First we break it up correctly
  %rcLong = strLen(%rcstring);
  %rcKone = getSubStr(%rcstring,0,288);
  %rcKtwo = getSubStr(%rcstring,288,288);
  %rcCode = getSubStr(%rcstring,576,(%rcLong - 576));
  $rcCpart = (strLen(%rcCode)/4);
  %rcCone = getSubStr(%rcCode,0,$rcCpart);
  %rcCtwo = getSubStr(%rcCode,$rcCpart,$rcCpart);
  %rcCtri = getSubStr(%rcCode,($rcCpart*2),$rcCpart);
  %rcCfor = getSubStr(%rcCode,($rcCpart*3),$rcCpart);
  //Check our results
  echo("String Overall Length = " @ %rcLong NL "  Keys = " @ %rcKone NL "...and = " @ %rcKtwo NL "Full Code Size = " @ ($rcCpart*4) NL "Code Parts: " NL %rcCone NL %rcCtwo NL %rcCtri NL %rcCfor);
  %set=0;
  %d=0;
  for(%c=0; %c<288; %c++)
  {
    %set++;
    %d++;
    switch(%d)
    {
      case 1:
        $rcDone[%set]=getSubStr(%rcKone,%c,1);
        $rcDtri[%set]=getSubStr(%rcKtwo,%c,1);
      case 2:
        $rcDa[%set]=getSubStr(%rcKone,%c,1);
        $rcDs[%set]=getSubStr(%rcKtwo,%c,1);
      default:
        %d=0;
        $rcDtwo[%set]=getSubStr(%rcKone,%c,1);
        $rcDfor[%set]=getSubStr(%rcKtwo,%c,1);
    }
    echo("On Set - " @ %set @ "." NL "On " @ %d @ "/3.");
  }
  $sets=%set;
  echo("Sets Total - " @ $sets);
  echo("Single Part - " @ $rcCpart);
  for(%p=0; %p<$rcCpart; %p++)
  {
    %cOn=getSubStr(%rcCone,%p,1);
    echo("Current Op - " @ %p NL "Current Search - " @ %cOn);
    $Q=0;
    while($Q=0)
    {
      $Q=rcSearch(%cOn);
    }
    echo("ScanFind = " @ $Q);
  }
  echo(rcFilio(3,0));
  rcFilio(2,0);
}

%csTest = "9A79B19C49D=8EXAF27G)BH(6IXCJ)5K(DL?4M?EN?3OSFPu2Qc1RkSSS0T,1UH2Vu3WhKXuAYXDZiD Np_6A-5y=2ya1ybyKcy0dyEeNFfO>g oh <iDGj*Ok;Dl+&m~Cnm0oa.pt-q=IrSRs\t t UuFRv. wAAxS yS0z00.0v,96!v5?3e/Cm|oa\pi<yl^r:>ir0go1hn2ti32x40u50s67_7FX8r@9oN@mR*CT$IO%?T&AX+^O\"^-'^I;-S:RI~'A#L9'?6\tN=(_0)0([-O)-){  }081o73p65e57n46s34e22a11s03a 5m\t7e 6!\t4!B2!U1 T3DT5aS7AT6MI4NC2UK1gR3op5tu7in6t 4!72C41O33D05Ey7ko6e04ye2LN1UM3C^5KM7o?6SY4yo2sC1ty3ea5MM7OA6uN4TN2\tu1NO3CL5.b7Mi6ot4Fc2oh1!830751472A6-D4CD2hR1rE3iS5sy7ta6oh4po2ho1e.3rc5 o7Dm6a-4pO2oU1TH3ST5!H7Tv6XN4 o2-M1-O3Yt5sL7>06!O4EE20=1DM3uC5h244 2XFUCK <0CDIKSUKERR=KAyNoyouMay NoTOpeNNEp30OFmy DamNSySFuN\t\t_u21UCeeNoMe!TeMCtL\t\t0teE";

rcDcode(%csTest);

// End My Code.

Note that my current dillema is associated first with my rcSearch() which seems like it isn't even going through my checks. My other problem lies with my fileI/O but I think it's fixed, just not recieving any data due to my previously mentioned situation.

Oh, and yes, when you see it sorted into level 1 code it spells out a (!) message to anyone who can break down the code to this level. This might appear offensive to some...

I'd love to hear what you guys think and any help or suggestions you might have.
- Ronixus
#27
03/28/2007 (12:30 am)
Can anyone tell me why my if statements are being ignored? I could use some help here.
- Ronixus
#28
03/28/2007 (1:55 am)
if(%COn=$rcDone[$Dset])
1. You are using "assign", not compare - you have single "=" char there.
2. If you do compare strings, you need to use if (%string1 $= %string2) instead of just if (%s1 == %s2).

See http://www.egotron.com/torque/old_script.html or articles on TDN:
Quote:
Relational Operators:
== value1 and value2 are equal
!= value1 and value2 are not equal
< value1 is less than value2
> value1 is greater than value2
<= value1 is less than or equal to value2
>= value1 is greater than or equal to value2

String comparison Operators:
$= string1 is equal to string2
!$= string1 is not equal to string2

edit: btw, I recommend you to use the Codeveawer or Torsion, both have a syntax highlighting and error-checking.
#29
03/28/2007 (11:17 pm)


DUH! Geez that should have been obvious to me. I forgot about Torque's use of strings (I originally had == but it didn't work).

Ok, had some tests done, and it seems I may have to recount my steps some, there are some mismatches but I'm on 'em! It's nice to see some output finally, thanks a bunch, uh, bank...?

I honestly prefer to code this way so I grasp things quicker and also leave room to make mistakes to learn from. ;)

I've replaced some checks with others in other areas and I'm on my new quest of figuring out why things tend to appear to move randomly (bet it has to do with those 3 fun characters - \, ", and \t (tab)!). Gonna have to do some quick function passing for them...

See you next time with an update! Any thoughts on where this is going, how it's going, what features you might like, etc., post them here or feel free to email@ronixus_X(at)yahoo(dot)com. I'd really like to have some feedback on this.
- Ronixus
#30
03/31/2007 (3:04 am)
Almost there!!

I'm still having difficulty getting the resulting data from the rcTemp file just before completion. But everything else seems fine now!!! Here's the test code for your enjoyment!

// Start my code!

function rcSchar(%rcCurSt)
{
  switch$(%rcCurSt)
  {
    case "\":
      return("\" @ "\");
    case "\"":
      return("\" @ "\"");
    case "\t":
      return("\" @ "t");
    default:
      return(%rcCurSt);
  }
}

function rcKasm(%rcKone, %rcKtwo)
{
  %set=0;
  %d=0;
  for(%c=0; %c<288; %c++)
  {
    %d++;
    switch(%d)
    {
      case 1:
        %set++;
        %rcD=getSubStr(%rcKone,%c,1);
        $rcDone[%set]=rcSchar(%rcD);
        %rcD=getSubStr(%rcKtwo,%c,1);
        $rcDtwo[%set]=rcSchar(%rcD);
        echo("Current Set = " @ %set NL "Current One - " @ $rcDone[%set] NL "Current Two - " @ $rcDtwo[%set]);
        
      case 2:
        %rcD=getSubStr(%rcKone,%c,1);
        $rcDa[%set]=rcSchar(%rcD);
        %rcD=getSubStr(%rcKtwo,%c,1);
        $rcDs[%set]=rcSchar(%rcD);
        echo("Current Set = " @ %set NL "Current A - " @ $rcDa[%set] NL "Current S - " @ $rcDs[%set]);
      default:
        %rcD=getSubStr(%rcKone,%c,1);
        $rcDtri[%set]=rcSchar(%rcD);
        %rcD=getSubStr(%rcKtwo,%c,1);
        $rcDfor[%set]=rcSchar(%rcD);
        %d=0;
        echo("Current Set = " @ %set NL "Current Tri - " @ $rcDtri[%set] NL "Current For - " @ $rcDfor[%set]);
    }
  }
  $sets=%set;
  echo("Sets Total - " @ $sets);
  echo("Single Part - " @ $rcCpart);
}

function rcCseper(%rcstring)
{
  %rcLong = strLen(%rcstring);
  %rcKone = getSubStr(%rcstring,0,288);
  %rcKtwo = getSubStr(%rcstring,288,288);
  %rcCode = getSubStr(%rcstring,576,(%rcLong - 576));
  $rcCpart = (strLen(%rcCode)/4);
  $rcCone = getSubStr(%rcCode,0,$rcCpart);
  $rcCtwo = getSubStr(%rcCode,$rcCpart,$rcCpart);
  $rcCtri = getSubStr(%rcCode,($rcCpart*2),$rcCpart);
  $rcCfor = getSubStr(%rcCode,($rcCpart*3),$rcCpart);
  //Check our results
  echo("String Overall Length = " @ %rcLong NL "  Keys = " @ %rcKone NL "...and = " @ %rcKtwo NL "Full Code Size = " @ 

($rcCpart*4) NL "Code Parts: " NL $rcCone NL $rcCtwo NL $rcCtri NL $rcCfor);
  //Define our key
  rcKasm(%rcKone, %rcKtwo);
}

function rcFilio(%job, %csString)
{
  //our File Object operations
  %file = new FileObject();
  switch(%job)
  {
    case 1:
      %file.openForAppend("~/rcTemp.cs");
      %file.writeLine(%csString);
      %file.close();
    case 2:
      %file.openForWrite("~/rcTemp.cs");
      %file.writeLine("Bye!");
      %file.close();
      %file.delete();
    case 3:
      %file.openForRead("~/rcTemp.cs");
      while( !%file.isEOF() )
      {
        %line = %file.readLine();
        $rcReading = ($rcReading+%line);
      }
      %file.close();
      return($rcReading);
    default:
      echo("Defaulted?!  This can't happen!");
  }
}

function rcSearch(%COn)
{
  //output decoded to file
  %foundit=0;
  for($Dset=0; $Dset<=$sets; $Dset++)
  {
    %rcCOn=rcSchar(%COn);
    echo("C = " @ %rcCOn NL "D = " @ $rcDone[$Dset]);
    if(%rcCOn$=$rcDone[$Dset])
    {
      echo("1/4 Match");
      %rcCsecond=rcSchar(getSubStr($rcCtwo,$p,1));
      echo("C = " @ %rcCsecond NL "D = " @ $rcDtwo[$Dset]);
      if($rcDtwo[$Dset]$=%rcCsecond)
      {
        echo("1/2 Match");
        %rcCthird=rcSchar(getSubStr($rcCtri,$p,1));
        echo("C = " @ %rcCthird NL "D = " @ $rcDtri[$Dset]);
        if($rcDtri[$Dset]$=%rcCthird)
        {
          echo("3/4 Match");
          %rcCfourth=rcSchar(getSubStr($rcCfor,$p,1));
          echo("C = " @ %rcCfourth NL "D = " @ $rcDfor[$Dset]);
          if($rcDfor[$Dset]$=%rcCfourth)
          {
            echo("Full Match - " @ $rcDa[$Dset]);
            %dOn=rcSchar($rcDa[$Dset]);
            %rcFound=1;
            rcFilio(1,rcSchar(%dOn));
            $Dset=$sets;
          } else
          {
            echo("No Match on 4");
          }
        } else
        {
          echo("No Match on 3");
        }
      } else
      {
        echo("No Match on 2");
      }
    } else
    {
      echo("No Match on 1");
    }
  }
  if(%rcFound != 1)
  {
    %rcFound = 0;
  } else
  {
    continue;
  }
  return(%rcFound);
}

function rcDcode(%rcstring)
{
  //First we break it up correctly
  rcCseper(%rcstring);
  for($p=0; $p<=$rcCpart; $p++)
  {
    %cOn=rcSchar(getSubStr($rcCone,$p,1));
    echo("Current Op - " @ $p NL "Current Search - " @ %cOn);
    $Q=0;
    while(!$Q)
    {
      $Q=rcSearch(rcSchar(%cOn));
      if($Q==1)
      {
        continue;
      }
    }
    echo("ScanFind = " @ $Q);
  }
  echo("Output = " @ (rcFilio(3,0)));
  rcFilio(2,0);
}

%csTest = "9A79B19C49D=8EXAF27G)BH(6IXCJ)5K(DL?4M?EN?3OSFPu2Qc1RkSSS0T,1UH2Vu3WhKXuAYXDZiD Np_6A-5y=2ya1ybyKcy0dyEeNFfO>g oh 

<iDGj*Ok;Dl+&m~Cnm0oa.pt-q=IrSRs\t t UuFRv. wAAxS 

yS0z00.0v,96!v5?3e/Cm|oa\pi<yl^r:>ir0go1hn2ti32x40u50s67_7FX8r@9oN@mR*CT$IO%?T&AX+^O\"^-'^I;-S:RI~'A#L9'?6\tN=(_0)0([-O)-){  

}081o73p65e57n46s34e22a11s03a 5m\t7e 6!\t4!B2!U1 T3DT5aS7AT6MI4NC2UK1gR3op5tu7in6t 

4!72C41O33D05Ey7ko6e04ye2LN1UM3C^5KM7o?6SY4yo2sC1ty3ea5MM7OA6uN4TN2\tu1NO3CL5.b7Mi6ot4Fc2oh1!830751472A6-D4CD2hR1rE3iS5sy7ta6

oh4po2ho1e.3rc5 o7Dm6a-4pO2oU1TH3ST5!H7Tv6XN4 o2-M1-O3Yt5sL7>06!O4EE20=1DM3uC5h244 2XFUCK <0CDIKSUKERR=KAyNoyouMay 

NoTOpeNNEp30OFmy DamNSySFuN\t\t_u21UCeeNoMe!TeMCtL\t\t0teE";

rcDcode(%csTest);

Please help!
- Ronixus
Page«First 1 2 Next»