RE: Converting a string to a variable name

new topic     » goto parent     » topic index » view thread      » older message » newer message

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C236A9.121BF430
 charset=iso-8859-1

I know a lot of people have already responded to this, but may I add my
slant too.

> -----Original Message-----
> From: rubis at fem.unicamp.br [mailto:rubis at fem.unicamp.br]
> Subject: Converting a string to a variable name
> 
> 
> 
> Hi all;
> 
> Direct to the point...
> 
> 	I'm writing a program that uses a lot of sequencial variables:
> 
> a1, a2, a3...

And here is the key concept - 'sequential variables' - This is also known as
an array in programming term. In Euphoria, an array can be implemented using
a sequence.

Thus  'a1' is the first 'a', 'a2' is the second 'a', 'a3' is the third 'a',
etc...

So, if you know how many 'a's you need you can do this...

  sequence a
  a = repeat(0, maxA)

> and give values for then:
> 
> a1=1, a2=2, a3=3 ...(for example)
> 

  a[1] = 1
  a[2] = 2
  a[3] = 3

> I would like to do this:
> 
> atom a1,a2,a3
> sequence s
> 	for x=1 to 3 do
> 
> 		s=sprintf("%d",x)
> 		"a"&s=x                           -- atribute a 

    for x = 1 to length(a) do
        a[x] = x
    end for

> "variable variable" name 
> a variable value  :>))
> 
> 	end for
> 
> but this does not work because "a"&s is a string, not a variable.
> 
> How I can convert a string to a variable name ???

If you are using sequential variables, you don't have to.

Another type of array, is one where the elements are NOT sequential. That
is, the elements are not referenced by number, but by something else - a
string for example.

  a["ant"] = 1
  a["bat"] = 2
  a["kat"] = 3
  a["dog"] = 4
     ... etc...

Unfortunately, Euphoria doesn't directly support this idea. However, it can
support it indirectly.

  sequence names
  sequence a

   names = {"ant", "bat", "kat", "dog"}
   a = repeat(0, length(names))
 
  a[find("ant", names)] = 1
  a[find("bat", names)] = 2
  a[find("kat", names)] = 3
  a[find("dog", names)] = 4


  --To add a new named element...
   names = append(names, "emu")
   a     = append(a,     0)

The above is alright so long as you always use the correct names. As a
simple trick to cater for wrong names being used...


  sequence names
  sequence a

   names = {"ant", "bat", "kat", "dog"}
   a = repeat(0, length(names) + 1) -- Add an extra to handle bad names.
 
  a[find(The_Name, names) + 1] = New_Value

The '+ 1' is used so that if the value in The_Name is not a valid name, then
the first element is assigned. If the name is valid then its correct
position is assigned - the first name uses the second 'a', the second name
uses the third 'a', etc...

Alternatively, you might do it this way...
   integer idx

   idx = find(The_Name, names)
   if idx then
       a[idx] = New_Value
   else
       errmsg "Wrong name used"
   end if

---------------
hth,
Derek


   

==================================================================
De informatie opgenomen in dit bericht kan vertrouwelijk zijn en 
is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht 
onterecht ontvangt wordt u verzocht de inhoud niet te gebruiken en 
de afzender direct te informeren door het bericht te retourneren. 
==================================================================
The information contained in this message may be confidential 
and is intended to be exclusively for the addressee. Should you 
receive this message unintentionally, please do not use the contents 
herein and notify the sender immediately by return e-mail.


==================================================================

------_=_NextPart_000_01C236A9.121BF430
Content-Type: application/ms-tnef

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu