1. RE: Converting a string to a variable name
- Posted by irv at take.maxleft.com
Jul 28, 2002
rubis at fem.unicamp.br wrote:
> Hi all;
>
> Direct to the point...
>
> I'm writing a program that uses a lot of sequencial variables:
>
> a1, a2, a3...
>
> and give values for then:
>
> a1=1, a2=2, a3=3 ...(for example)
>
>
> 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 "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 ??
You can't, but is there any reason you cannot use an array of
atoms?
object a
a[1] = 1
a[2] = 2
...etc
for x = 1 to 3 do
a[x] = ??
Irv
2. RE: Converting a string to a variable name
- Posted by irv at take.maxleft.com
Jul 28, 2002
irv at take.maxleft.com wrote:
>
> rubis at fem.unicamp.br wrote:
> > Hi all;
> >
> > Direct to the point...
> >
> > I'm writing a program that uses a lot of sequencial variables:
> >
> > a1, a2, a3...
> >
> > and give values for then:
> >
> > a1=1, a2=2, a3=3 ...(for example)
> >
> >
> > 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 "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 ??
>
> You can't, but is there any reason you cannot use an array of
> atoms?
>
> object a
> a[1] = 1
> a[2] = 2
> ...etc
>
> for x = 1 to 3 do
> a[x] = ??
>
I suppose I should mention that you'll need to either dimension the
array before using it:
a = repeat(0,4)
or
a = {0,0,0,0}
or
a = {1,2,3,4} -- create and initialize in one step.
If you don't know how many numbers you'll be storing, you
can create an 'empty' array:
a = {}
and then append each new number to it.
a = {}
for x = 1 to 4 do
a = append(a,x)
end for
a will now look like this: {1,2,3,4}
Irv
3. RE: Converting a string to a variable name
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
4. RE: Converting a string to a variable name
Derek Parnell wrote:
> Another type of array, is one where the elements are NOT sequential.
[snip]
> Unfortunately, Euphoria doesn't directly support this idea.
Fortunately there is a library called "Associative Lists" by Jiri
Barbor that can do it as well :)
http://www.rapideuphoria.com/list.zip
Ray Smith
http://rays-web.com
5. RE: Converting a string to a variable name
On 29 Jul 2002, at 5:55, Ray Smith wrote:
>
>
> Derek Parnell wrote:
>
> > Another type of array, is one where the elements are NOT sequential.
>
> [snip]
>
> > Unfortunately, Euphoria doesn't directly support this idea.
>
> Fortunately there is a library called "Associative Lists" by Jiri
> Barbor that can do it as well :)
>
> http://www.rapideuphoria.com/list.zip
Oh cool, that is like the code i posted earlier here!
Kat
6. RE: Converting a string to a variable name
Here's my two [late] cents, since I didn't check my mail this weekend.
> -----Original Message-----
> From: rubis at fem.unicamp.br [mailto:rubis at fem.unicamp.br]
> Hi all;
>
> Direct to the point...
>
> I'm writing a program that uses a lot of sequencial variables:
>
> a1, a2, a3...
>
> and give values for then:
>
> a1=1, a2=2, a3=3 ...(for example)
>
>
> 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
> "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 ???
You could do exactly this with my modified interpreter (check the recent
contributions). Although this particular interest would be better served
simply using arrays, as many have pointed out, you could also do this:
-- example
include variable.em
atom a1, a2, a3
sequence s
integer id
for x = 1 to 3 do
s = sprintf("%d",x)
id = variable_id( "a" & s )
if id >= 0 then
write_variable( id, x )
end if
end for
-- end example
Matt Lewis
7. RE: Converting a string to a variable name
Hey all...
Sorry for posting late.
This is quite doable, even in Euphoria (regardless that others may
nay-say it). I do not have time to write up the code for it atm, but
gimme about a day or two and I will show you how you can get around
Euphorias short coming in this area. If I had known sooner that there
was such a need for it I would have posted something about it earlier
(sorry Kat!).
Best regards
Don Phillips
8. RE: Converting a string to a variable name
On 29 Jul 2002, at 14:30, Don Phillips wrote:
>
> Hey all...
> Sorry for posting late.
>
> This is quite doable, even in Euphoria (regardless that others may
> nay-say it). I do not have time to write up the code for it atm, but
> gimme about a day or two and I will show you how you can get around
> Euphorias short coming in this area. If I had known sooner that there
> was such a need for it I would have posted something about it earlier
> (sorry Kat!).
It wasn't me who asked for a way to make var names *recently*, i have a
workaround, which i posted here. But i still say it should be in native
Euphoria.
Kat
9. RE: Converting a string to a variable name
> It wasn't me who asked for a way to make var names *recently*, i have a
> workaround, which i posted here. But i still say it should be in native
> Euphoria.
>
> Kat
Yeah I know Kat... there are probably 10 different ways to implement
this. It >would< be nice if this was native Euphoria, and it would also
be nice if Euphoria would at least handle structures like all the other
languages I have seen. Sequences are nice, but not when working with
the API. Oh well, heres a couple of routines you can use to emulate
dynamic variables through strings.
Note: The HashWin (window) I created is technically not needed. The
routines will attach a string variable to any window. I placed it in
the code to simplify the calling procedures/ functions.
===============
include Win32Lib.ew
constant
setProp = registerw32Procedure( user32, "SetPropA",
{C_POINTER,C_POINTER,C_LONG} ),
getProp = registerw32Function( user32, "GetPropA",
{C_POINTER,C_POINTER}, C_LONG ),
removeProp = registerw32Procedure( user32, "removePropA",
{C_POINTER,C_POINTER} )
constant
HashWin = create( Window, "", NULL, 0, 0, 1, 1, {0} ),
hWndHash = getHandle( HashWin )
procedure SetStrVar( sequence StrVar, atom Value )
atom hStrVar
hStrVar = allocate_string( StrVar )
w32Proc( setProp, {hWndHash,hStrVar,Value} )
free( hStrVar )
end procedure
function GetStrVar( sequence StrVar )
atom hStrVar
atom RetVal
hStrVar = allocate_string( StrVar )
RetVal = w32Func( getProp, {hWndHash,hStrVar} )
free( hStrVar )
return( RetVal )
end function
procedure RemoveVar( sequence StrVar )
atom hStrVar
hStrVar = allocate_string( StrVar )
w32Proc( removeProp, {hWndHash,hStrVar} )
free( hStrVar )
end procedure
-- usage
--
-- to set: SetStrVar( "VarName", Value )
-- to get: GetStrVar( "VarName" )
-- (optional)to remove: RemoveVar( "VarName" )
--
===============
Don Phillips
10. RE: Converting a string to a variable name
- Posted by rubis at fem.unicamp.br
Jul 29, 2002
Hi all !
Thanks all for the "lessons" :>)
As i'm starting in Euphoria, i'm always learning with this forum. The best
manual for Euphoria is here. :>)
Now I will explain why I cannot use arrays in my program :
This is my program: (Win32)
------------------------------------------------------------------------------------------------------------------------------------------
include Win32lib.ew
--------------------------------------------------------------------------------
-- Window Window1
constant Window1 = createEx( Window, "", 0, Default, Default, 602, 338, 0, 0 )
object etext1,etext2,etext3...
constant EditText1 = createEx( EditText, "100", Window1, 8, 32, 70, 20, 0, 0 )
constant EditText2 = createEx( EditText, "100", Window1, 8, 56, 70, 20, 0, 0 )
constant EditText3 = createEx( EditText, "100", Window1, 8, 80, 70, 20, 0, 0 )
.
.
.and so on... a lot.
I have to change the value "100" in the EditText1, EditText2... and so
on... to values from etext1, etext2 and so...
The only way i find to do this is:
etext1=gets(arquivo) --------some txt archive
etext1=etext1[1..length(etext1)-1]
setText(EditText1,etext1)
etext2=gets(arquivo)
etext2=etext2[1..length(etext2)-1]
setText(EditText2,etext2)
etext3=gets(arquivo)
etext3=etext3[1..length(etext3)-1]
.
.
.one by one.... and a ugly code.
This is what will be nice to do...
for x=1 to .. do ---- until the end of the file
etext&x=gets(arquivo)
setText(EditText&x,etext&x) ------------------------ set EditText1 to value
etext1
end if
nice code... but does not work.
I didn't try using arrays still now, but ...does this work ?
for x=1 to 10 do
etext[x]=gets(arquivo)
setText(EditText[x],etext[x]) ------------------------ set EditText1 to
value etext1
end if
By
Rubens
At 15:17 29/7/2002, you wrote:
>
> > It wasn't me who asked for a way to make var names *recently*, i have a
> > workaround, which i posted here. But i still say it should be in native
> > Euphoria.
> >
> > Kat
>
>Yeah I know Kat... there are probably 10 different ways to implement
>this. It >would< be nice if this was native Euphoria, and it would also
>be nice if Euphoria would at least handle structures like all the other
>languages I have seen. Sequences are nice, but not when working with
>the API. Oh well, heres a couple of routines you can use to emulate
>dynamic variables through strings.
>
>Note: The HashWin (window) I created is technically not needed. The
>routines will attach a string variable to any window. I placed it in
>the code to simplify the calling procedures/ functions.
>
>===============
>
>include Win32Lib.ew
>
>constant
>setProp = registerw32Procedure( user32, "SetPropA",
>{C_POINTER,C_POINTER,C_LONG} ),
>getProp = registerw32Function( user32, "GetPropA",
>{C_POINTER,C_POINTER}, C_LONG ),
>removeProp = registerw32Procedure( user32, "removePropA",
>{C_POINTER,C_POINTER} )
>
>constant
>HashWin = create( Window, "", NULL, 0, 0, 1, 1, {0} ),
>hWndHash = getHandle( HashWin )
>
>procedure SetStrVar( sequence StrVar, atom Value )
> atom hStrVar
> hStrVar = allocate_string( StrVar )
> w32Proc( setProp, {hWndHash,hStrVar,Value} )
> free( hStrVar )
>end procedure
>
>function GetStrVar( sequence StrVar )
> atom hStrVar
> atom RetVal
> hStrVar = allocate_string( StrVar )
> RetVal = w32Func( getProp, {hWndHash,hStrVar} )
> free( hStrVar )
> return( RetVal )
>end function
>
>procedure RemoveVar( sequence StrVar )
> atom hStrVar
> hStrVar = allocate_string( StrVar )
> w32Proc( removeProp, {hWndHash,hStrVar} )
> free( hStrVar )
>end procedure
>
>-- usage
>--
>-- to set: SetStrVar( "VarName", Value )
>-- to get: GetStrVar( "VarName" )
>-- (optional)to remove: RemoveVar( "VarName" )
>--
>
>===============
>
>Don Phillips
>
>
>
11. RE: Converting a string to a variable name
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_01C2375F.7F5AE7C0
charset=iso-8859-1
Hi Rubens,
There is always more than one way to achieve your goals in programming. Here
is a method that I might've used...
include Win32lib.ew
--------------------------------------------------------------
------------------
-- Window Window1
constant Window1 = createEx( Window, "", 0, Default, Default, 602, 338, 0,
0 )
sequence etid etid = {}
etid &= createEx( EditText, "100", Window1, 8, 32, 70, 20, 0, 0 )
etid &= createEx( EditText, "100", Window1, 8, 56, 70, 20, 0, 0 )
etid &= createEx( EditText, "100", Window1, 8, 80, 70, 20, 0, 0 )
.
.
.and so on... a lot.
object etext
integer cnt
etext=gets(arquivo) --------some txt archive
cnt = 1
while sequence(etext) and cnt <= length(etid) do
etext=etext[1..length(etext)-1]
setText(etid[cnt],etext)
etext = gets(arquivo)
cnt += 1
end while
> -----Original Message-----
> From: rubis at fem.unicamp.br [mailto:rubis at fem.unicamp.br]
> Sent: Tuesday, 30 July 2002 10:00
> To: EUforum
> Subject: RE: Converting a string to a variable name
>
>
>
> Hi all !
> Thanks all for the "lessons" :>)
> As i'm starting in Euphoria, i'm always learning with this
> forum. The best
> manual for Euphoria is here. :>)
>
> Now I will explain why I cannot use arrays in my program :
>
> This is my program: (Win32)
> --------------------------------------------------------------
> --------------------------------------------------------------
> --------------
> include Win32lib.ew
> --------------------------------------------------------------
> ------------------
> -- Window Window1
> constant Window1 = createEx( Window, "", 0, Default, Default,
> 602, 338, 0, 0 )
>
> object etext1,etext2,etext3...
>
> constant EditText1 = createEx( EditText, "100", Window1, 8,
> 32, 70, 20, 0, 0 )
> constant EditText2 = createEx( EditText, "100", Window1, 8,
> 56, 70, 20, 0, 0 )
> constant EditText3 = createEx( EditText, "100", Window1, 8,
> 80, 70, 20, 0, 0 )
> .
> .
> .and so on... a lot.
>
> I have to change the value "100" in the EditText1,
> EditText2... and so
> on... to values from etext1, etext2 and so...
>
> The only way i find to do this is:
>
> etext1=gets(arquivo) --------some
> txt archive
> etext1=etext1[1..length(etext1)-1]
> setText(EditText1,etext1)
> etext2=gets(arquivo)
> etext2=etext2[1..length(etext2)-1]
> setText(EditText2,etext2)
> etext3=gets(arquivo)
> etext3=etext3[1..length(etext3)-1]
> .
> .
> .one by one.... and a ugly code.
>
> This is what will be nice to do...
>
> for x=1 to .. do ---- until the end of the file
> etext&x=gets(arquivo)
> setText(EditText&x,etext&x) ------------------------ set
> EditText1 to value
> etext1
> end if
>
> nice code... but does not work.
>
> I didn't try using arrays still now, but ...does this work ?
>
> for x=1 to 10 do
> etext[x]=gets(arquivo)
> setText(EditText[x],etext[x]) ------------------------ set
> EditText1 to
> value etext1
> end if
-------------
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_01C2375F.7F5AE7C0
Content-Type: application/ms-tnef