Page 1 of 2

and arrays again

Posted: Sat Nov 02, 2013 1:20 pm
by tester
Let say that there are two arrays:

1
2
3
4

a
b
c
d

And I'd like to get:

1 - a
2 - b
3 - c
4 - d

So it wold mean two inputs for two arrays, and custom separator " - " for formatting. How to make it in ruby? How to avoid an error, if two arrays are different in size?

Re: and arrays again

Posted: Sun Nov 03, 2013 12:42 am
by billv
This ones been fun tying to work out as well tester. A bit tricky though.
I did a version that works great, accounts for errors, but is string output,
not array like you need...but In the meantime, still might help... :mrgreen:
Tester array_single output version.fsm
(2.55 KiB) Downloaded 1056 times


I can see how the array output can be done...fun challenge.... :D

Re: and arrays again

Posted: Sun Nov 03, 2013 1:04 am
by RJHollins
Question?

Is the '-' anything more than formatting ? If not, you show a 2D array that would then be 'the' data ... and for display purposes you could handle alignment, spacing, '-', etc, all from GUI control [even PRIMS].

Just trying to understand the need.

Re: and arrays again

Posted: Sun Nov 03, 2013 1:10 am
by Nubeat7
@tester:

Code: Select all

@c= []
0.upto @a.length-1 do |i|
   @c[i] = (@a[i].to_s)+"-"+(@b[i].to_s)
end
output @c


but you need to fix the arrays before with trogs fix_array_size methode

Re: and arrays again

Posted: Sun Nov 03, 2013 1:12 am
by billv
look at testers post again...his needs are very clear...

at the moment , tying to map em both together via loop....
EDIT:
Just saw your post..
Thanks nubeat..will have a go...

Re: and arrays again

Posted: Sun Nov 03, 2013 2:07 am
by strangeChild
Green is not dead...
If it wasn't for the white-space these few primitives would do the trick nicely.

Re: and arrays again

Posted: Sun Nov 03, 2013 5:49 am
by trogluddite
tester wrote:How to make it in ruby? How to avoid an error, if two arrays are different in size?

Depends which you prefer...
- Default value adding to the "short" array.
- Take away surplus from the "long" array.

Re: and arrays again

Posted: Sun Nov 03, 2013 8:29 am
by billv
This was as far as i got..

Code: Select all

def event i,v

if i == 0 then

x = 0
data = Array.new
for i in 1..@a1.length do
data[x] = i = @a1.map.to_s{|x| x}+("-")+ @a2.map.to_s{|x| x}
x += 1
output 0,data
end

end
end

...and result...

Code: Select all

#<Enumerator:0x3abce3c>-#<Enumerator:0x3abcd60>
#<Enumerator:0x3abccac>-#<Enumerator:0x3abcb80>
#<Enumerator:0x3abcb30>-#<Enumerator:0x3abcae0>
#<Enumerator:0x3abca90>-#<Enumerator:0x3abca18>
#<Enumerator:0x3abc978>-#<Enumerator:0x3abc8d8>

:lol: ..

strangeChild wrote:Green is not dead...

nice circuit...
Thanks for the reminder of how much i miss working in green... :)

Re: and arrays again

Posted: Sun Nov 03, 2013 11:55 am
by tester
In greenery I'm doing it like this (see attachement).

I prefer to recalculate the "pool" each time it changes. I could generate one larger, once - and use "string array split", I'm aware of that, but I would have to remember about that limitation too. I'm using this one, because it's easier to adapt, depending on where it is used, and it has no limitation (less to remember - less to worry about). On the other hand - it's slower, or it adds some slowdowns when it's among others.

Now - if the pool is dynamic in size, large, formatting changes "on-the-fly" per click for some reason (don't ask me "why" - that's the part of my movie, where I'm predicting "possible scenarios"), and it's clicked often, then this one adds it's own slowdown to the whole project.

It is used for combinig display and for saving. Not for loading (in terms of processing), because items have associated indexes used separately.

*

So my original question was regarding general simplification. Combine array1 and array2, and place between them a mark with whitespaces (these are a nghtmare sometimes). Add mark with witespaces always after array1, and always before array2, while array1 and array2 may have different amounts of elements.

*

I know I'm doing it in reversed order, but just wanted to post some example. Now I will read and check what you wrote above. :)

Re: and arrays again

Posted: Sun Nov 03, 2013 12:03 pm
by tester
Nubeat7 wrote:@tester:

Code: Select all

@c= []
0.upto @a.length-1 do |i|
   @c[i] = (@a[i].to_s)+"-"+(@b[i].to_s)
end
output @c


but you need to fix the arrays before with trogs fix_array_size methode


This one works reasonably fine, thanks. First array defines formatting and output size, second is just added and trimmed if it's larger. Actually this trimming might be useful too. White spaces are simply to add (" - " works).