Author Topic: More array fun... concatenate array values into a single integer  (Read 2394 times)

0 Members and 1 Guest are viewing this topic.

Offline Cloudee1

Ok, so I have an array with three values... and each array contains a value of 0,  4, 5, 6, 7, 32

So for example this would be a plausible scenario
  makeArray[1] = 0
  makeArray[2] = 32
  makeArray[3] = 7

Just so I can be sure to get a valid integer in the end, I plan to go ahead and make makeArray[0] = 1

Now, here is the easiest way to demonstrate what it is that I would like to accomplish... the outcome of this print statement would be 10327

Code: [Select]
FormatPrint ("%d%d%d%d" makeArray[0] makeArray[1] makeArray[2] makeArray[3])

except instead of printing the value to the screen what I would like to do is run it through a switch...
Code: [Select]
(switch(concatenatedArray)
 (case 1000 ...
 (case 1004 ...
 (case 1040 ...
 (case 1044 ...
 (case 1440 ...
 (case 1400 ...
 (case 1404 ...
 (case 1444 ...
)

But I am drawing a blank as to how to accomplish this concatenation.


Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline gumby

Re: More array fun... concatenate array values into a single integer
« Reply #1 on: July 26, 2014, 11:41:14 PM »
If you know the number of integers you are adding together, this isn't too bad.  Just multiply by tens, hundreds, thousands (you get it) to get the correct number and assign it to a variable, then do your case statement:

1000 + makearray[1] * 100 + makearray[2] * 10 + makearray[3]

(1000 + 0 + 32 + 7)

Not in 'infix' notation, but you get the idea.

EDIT:  NO, totally wrong.  What I think you are missing is using the 'Format' command instead and storing the result in the first variable.  From SCI Studio help file:


Format
string Format(string destString, string fmtString[, parameters])
 
My experience has been that your destString must be any array and you must use the @ character in the Format() call.
« Last Edit: July 26, 2014, 11:56:10 PM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Cloudee1

Re: More array fun... concatenate array values into a single integer
« Reply #2 on: July 27, 2014, 12:04:47 AM »
I may try it out sometime just for the sake of knowledge, but for now I have just cheated and gone about it the long way and created a nested switch... really it ends up being just about the same amount of coding since I need to have a value for all of the true combinations in either programming scenario. The only real difference is that I had to add a few more default statements as a result of the nesting. So either way for now, progress continues. Funny though, I keep asking questions but I'm not willing to wait for an answer before I hack it out the long way.  :P

By the way, your first suggestion would have worked just fine if I wouldn't have had that 32 in there as a possible value. If they would have all been 0-9 though I would have been good.
« Last Edit: July 27, 2014, 12:26:53 AM by Cloudee1 »
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.058 seconds with 21 queries.