Author Topic: String Manipulation  (Read 4424 times)

0 Members and 1 Guest are viewing this topic.

Offline MRide

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 711
  • Rating: +14/-0
  • You can't see this.
    • View Profile
String Manipulation
« on: July 21, 2010, 11:18:01 am »
I've written the following code in BASIC:

Code: [Select]
randInt(1,9->X
prgmNUM2STR
Str3->Str1
randInt(0,9->X
prgmNUM2STR
While Str3=Str1
randInt(0,9->X
prgmNUM2STR
End
Str3->Str2
randInt(0,9->X
prgmNUM2STR
While Str3=Str2 or Str3=Str1
randInt(0,9->X
prgmNUM2STR
End
Str1+Str2+Str3->Str1

I would like to convert this code into Axe.  I know the randInt(s can be converted to rand^10->X, and I've replaced prgmNUM2STR with sub(NS).
If I'm using it correctly, I believe I can change Str3->Str1 into Copy(Str3,Str1,1) (I'm not entirely sure though.)
I am having the most trouble with Str1+Str2+Str3->Str1.

Also, If you know of a better way to generate a random three digit number where no two digits are the same, and store it as a string, that would be most appreciated.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: String Manipulation
« Reply #1 on: July 21, 2010, 11:30:07 am »
A better way is like this: (I'll explain it in a moment, and note I'm storing the data at L1 rather than Str1)
Code: [Select]
rand^10+48->T->{L1
rand^9+48->S+(S>=T)->R->{L1+1
rand^8+48->S+(S>=T)->S+(S>=R)->{L1+2
0->{L1+3
It uses three variables for temporary purposes (R, S, and T) and the result is stored at L1
If you must use Str1, (you shouldn't have to) replace every L1 with Str1, add Zeros(4)->Str1 somewhere in your code, and remove the last line.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: String Manipulation
« Reply #2 on: July 21, 2010, 12:02:01 pm »
That code will not work, calcdude.  The first routine will return a random number 0-9, the second, 0-8, the third ,0-7.  In other words, they could still all end up being 7 or something.

[EDIT] Ignore this post.
« Last Edit: July 21, 2010, 12:07:57 pm by graphmastur »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: String Manipulation
« Reply #3 on: July 21, 2010, 12:03:04 pm »
that's what +(S>=T) and related are for. They pretty much skip over already-generated numbers.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: String Manipulation
« Reply #4 on: July 21, 2010, 12:07:30 pm »
Okay, I see it.  I see how it works.  Wow, that is really amazing.  Wow, my bad.

Offline MRide

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 711
  • Rating: +14/-0
  • You can't see this.
    • View Profile
Re: String Manipulation
« Reply #5 on: July 21, 2010, 12:12:52 pm »
Yes, this is great. Thanks, calcdude.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: String Manipulation
« Reply #6 on: July 21, 2010, 12:18:05 pm »
You're welcome. Glad to help! :)
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: String Manipulation
« Reply #7 on: July 21, 2010, 12:21:54 pm »
That's some neat code Calcdude!  Nice job! ;D