1. Dynamic names

Hi everyone,

Case:     In a given situation I want to add 1 to an specified integer.

Something like this:

integer atbat, RBIplayer1, RBIplayer2, RBIplayer3.....RBIplayer 10, etc.

if X > 0 then
     if atbat = player1 then RBIplayer1 = RBIplayer1 + 1 end if
     if atbat = player2 then RBIplayer2 = RBIplayer2 + 1 end if
....
     if atbat = player10 then RBIplayer10 = RBIplayer10 + 1 end if
end if

Is it possible to simplify it to something like this:

if X > 0 then
     RBI[atbat] = RBI[atbat] + 1
end if


Bye,

Jasper.

new topic     » topic index » view message » categorize

2. Re: Dynamic names

Jasper Holshuijsen wrote:

> Is it possible to simplify it to something like this:
> 
> if X > 0 then
>      RBI[atbat] = RBI[atbat] + 1
> end if

Urm, yes... it seems simple enough to test, am I missing something here?

If you're feeling especially reckless, you can also write:

   RBI[atbat] += (X > 0)

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

3. Re: Dynamic names

Hi David,

I tried
RBI[atbat] = RBI[atbat] + 1
and it didn't work. I thought I was using a wrong statement. I can't give
you the error message right now because I'm not behind the computer on
which I'm developing my program. Can I conclude from your reaction that I'm
on the right way? (For this case then smile)

Bye,

Jasper.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Dynamic names

Hi Jasper,
----- Original Message -----
From: <Jasper_Holshuijsen at dsw.nl>
To: "EUforum" <EUforum at topica.com>
Subject: Dynamic names


>
> Case:     In a given situation I want to add 1 to an specified integer.
>
> Something like this:
>
> integer atbat, RBIplayer1, RBIplayer2, RBIplayer3.....RBIplayer 10, etc.
>
> if X > 0 then
>      if atbat = player1 then RBIplayer1 = RBIplayer1 + 1 end if
>      if atbat = player2 then RBIplayer2 = RBIplayer2 + 1 end if
> ....
>      if atbat = player10 then RBIplayer10 = RBIplayer10 + 1 end if
> end if
>
> Is it possible to simplify it to something like this:
>
> if X > 0 then
>      RBI[atbat] = RBI[atbat] + 1
> end if
>
Sure is. This is a classic use of sequences. Remember that a sequence can be
used to implement an array. In this case a list of scores.

He is a bit of code to demonstrate it.

----------
sequence RBI
constant MAXPLAYERS = 10
integer atbat
. . .
-- Initialise the scores
RBI = repeat(0, MAXPLAYERS)
. . .
-- Increase score
if X > 0 then
    if atbat > 0 and atbat <= MAXPLAYERS then
        RBI[atbat] += 1
    else
        printf(1, "Logic error: 'atbat' = %d\n", atbat)
    end if
end if
------------

Derek.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Dynamic names

Thanks,

I haven't tried it yet but I think an array is exactly the answer on my
question.

Jasper.




Derek Parnell
<ddparnell@big       To:     EUforum <EUforum at topica.com>
pond.com>            cc:
Subject:     Re: Dynamic names
08-10-2001
13:43
Please respond
to EUforum







Hi Jasper,
----- Original Message -----
From: <Jasper_Holshuijsen at dsw.nl>
To: "EUforum" <EUforum at topica.com>
Sent: Monday, October 08, 2001 6:51 PM
Subject: Dynamic names


>
> Case:     In a given situation I want to add 1 to an specified integer.
>
> Something like this:
>
> integer atbat, RBIplayer1, RBIplayer2, RBIplayer3.....RBIplayer 10, etc.
>
> if X > 0 then
>      if atbat = player1 then RBIplayer1 = RBIplayer1 + 1 end if
>      if atbat = player2 then RBIplayer2 = RBIplayer2 + 1 end if
> ....
>      if atbat = player10 then RBIplayer10 = RBIplayer10 + 1 end if
> end if
>
> Is it possible to simplify it to something like this:
>
> if X > 0 then
>      RBI[atbat] = RBI[atbat] + 1
> end if
>
Sure is. This is a classic use of sequences. Remember that a sequence can
be
used to implement an array. In this case a list of scores.

He is a bit of code to demonstrate it.

----------
sequence RBI
constant MAXPLAYERS = 10
integer atbat
. . .
-- Initialise the scores
RBI = repeat(0, MAXPLAYERS)
. . .
-- Increase score
if X > 0 then
    if atbat > 0 and atbat <= MAXPLAYERS then
        RBI[atbat] += 1
    else
        printf(1, "Logic error: 'atbat' = %d\n", atbat)
    end if
end if
------------

Derek.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Dynamic names

Jasper Holshuijsen wrote:

> I tried
>    RBI[atbat] = RBI[atbat] + 1
> and it didn't work. I thought I was using
> a wrong statement.

One thing that can be really helpful is to use Euphoria's trace facility.
I've found it invaluable for debugging code.

> Can I conclude from your reaction that I'm
> on the right way? (For this case then smile)

Yes. smile

Good luck!

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu