[Wtr-general] OT: Programming/Ruby Logic question
Paul Carvalho
tester.paul at gmail.com
Fri Oct 27 18:37:07 EDT 2006
Nevermind. Problem solved. Nothing that a power nap and another hour of
exploratory programming couldn't work out. ;-)
I had to give my loop a 'memory'. Here's what I ended up doing:
----
first_seen = true
array.each_index do | position |
...
if array[ position ] == 'bar' and first_seen
start_col = 30
first_seen = false
elsif array[ position ] == 'bar' and ! first_seen
start_col = 40
end
...
end
----
This works in this example because there are at most only two occurrences of
'bar' in the array.
Now onto the next thing. Have a great weekend!
Cheers. Paul C.
On 27/10/06, Paul Carvalho <tester.paul at gmail.com> wrote:
>
> Hi there, I've been staring at a piece of code trying to get it to work
> for a while and I think I need a break. I have just one piece of the puzzle
> left to figure out and it's not really a Watir problem. Here's the problem
> simplified...
>
> I have an array with changing contents that I'm working with. For
> example:
>
> array = ['foo', 'bar']
> array = ['bar', 'bar']
>
> --> The relative positions are important here. The *first* occurrence of
> 'bar' should be implied to be "bar-1" and the second occurrence would be
> "bar-2".
>
> Now, the contents of these arrays are not only used to populate a text
> field on an input page somewhere, but they are also used as lookups in a
> spreadsheet to identify where the rest of the input data is. I'm trying to
> find a simple, elegant way to set the lookups but I'm running into a logic
> wall. (Very likely due to a lack of sleep.)
>
> Here are the lines of interest pertaining to the array data above:
> ----
> start_col = 0
> ...
> start_col = 30 if array[ position ] == 'bar' and ( something )
> start_col = 40 if array[ position ] == 'bar' and ( something_else )
> ----
>
> --> The part I'm having difficulty with is the "something" and
> "something_else". (FYI: 'position' is a local variable because these lines
> are in a loop that iterates through the elements of 'array'. So I need to
> tell the script that the first time you see 'bar' it means go to the first
> set of spreadsheet columns, but the second time you see it go to the second
> set of spreadsheet columns.)
>
> I originally had "something" check to see if it was the first element in
> the array, but as you can see from the first example above, that's not
> necessarily true. Then I tried having "something" check to see if the
> spreadsheet cell data for "bar-2" = 'None', but that's not necessarily true
> either ( i.e. see the second example).
>
> I'm just worried that I might have programmed myself into a corner. My
> request for help here is my last attempt at a way out before I rewrite a
> whole bunch of code so that this will work differently.
>
> Renaming the array elements (i.e. the "bar") to be unique would certainly
> solve this problem easily, but then it would break the code elsewhere.
>
> Any suggestions?
>
> Cheers. Paul C.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20061027/109be2a8/attachment-0001.html
More information about the Wtr-general
mailing list