1. Updating parameters in a procedure / function
I want to create a function that adds an element to a sequence, and returns
the index in the sequence. I want it to be flexible, so that it can
update any sequence - but how do I do that?
For example;
atom idx idx = 0
sequence myCars myCars = {}
sequence myPhones myPhones = {}
sequence myGirlFriends myGirlFriends = {}
function addElement(sequence Element, sequence mySequence)
mySequence = append(mySequence, Element)
return length(mySequence)
end function
idx = addElement("Audi", myCars)
idx = addElement("99 99 99 99", myPhones)
idx = addElement("Trine", myGirlFriends)
Regards,
Kenneth / ZNorQ
2. Re: Updating parameters in a procedure / function
ZNorQ wrote:
>
> I want to create a function that adds an element to a sequence, and returns
> the index in the sequence. I want it to be flexible, so that it can
> update any sequence - but how do I do that?
>
> For example;
>
> }}}
<eucode>
> atom idx idx = 0
> sequence myCars myCars = {}
> sequence myPhones myPhones = {}
> sequence myGirlFriends myGirlFriends = {}
>
> function addElement(sequence Element, sequence mySequence)
> mySequence = append(mySequence, Element)
> return length(mySequence)
> end function
>
> idx = addElement("Audi", myCars)
> idx = addElement("99 99 99 99", myPhones)
> idx = addElement("Trine", myGirlFriends)
> </eucode>
{{{
>
> Regards,
> Kenneth / ZNorQ
Hello ZNorQ,
It seems to me you would want:
atom idx idx = 0
sequence myCars myCars = {}
sequence myPhones myPhones = {}
equence myGirlFriends myGirlFriends = {}
function addElement(sequence Element, sequence mySequence)
mySequence = append(mySequence, Element)
return mySequence
end function
myCars=addElement("Audi",myCars)
idx=length(myCars)
myPhones = addElement("99 99 99 99", myPhones)
idx=length(myPhones)
myGirlFriends = addElement("Trine", myGirlFriends)
idx=length(myGirlFriends)
----------------also you could-------------------
function addElement(sequence Element, sequence mySequence)
mySequence = append(mySequence, Element)
return {mySequence }& {length(mySequence})
end function
junk=addElement("Audi",myCars)
myCars=junk[1]
idx=junk[2]
junk=addElement("99 99 99 ",myPhones)
myPhones=junk[1]
idx=junk[2]
junk=addElement("Trine", GirlFriends)
myGirlFriends=junk[1]
idx=junk[2]
>
Don Cole
3. Re: Updating parameters in a procedure / function
ZNorQ wrote:
> I want to create a function that adds an element to a sequence, and returns
> the index in the sequence. I want it to be flexible, so that it can
> update any sequence - but how do I do that?
>
> For example;
>
> }}}
<eucode>
> atom idx idx = 0
> sequence myCars myCars = {}
> sequence myPhones myPhones = {}
> sequence myGirlFriends myGirlFriends = {}
>
> function addElement(sequence Element, sequence mySequence)
> mySequence = append(mySequence, Element)
> return length(mySequence)
> end function
>
> idx = addElement("Audi", myCars)
> idx = addElement("99 99 99 99", myPhones)
> idx = addElement("Trine", myGirlFriends)
> </eucode>
{{{
I usually do something like this:
include misc.e
sequence globalList
globalList = {{},{}}
procedure createList(sequence name)
globalList[1] = append(globalList[1],name)
globalList[2] = append(globalList[2],{})
end procedure
function getListID(sequence name)
return find(name,globalList[1])
end function
procedure addItem(object item, object listID)
-- you'll want to create a test so that duplicates are managed
-- in the example below, the "Porsche" is added twice.
if atom(listID) then --using the list's index
globalList[2][listID] = append(globalList[2][listID],item)
else
listID = getListID(listID)
if listID > 0 then
globalList[2][listID] = append(globalList[2][listID],item)
end if
end if
end procedure
function addItemX(object item, object listID)
if sequence(listID) then
listID = getListID(listID)
end if
addItem(item,listID)
return length(globalList[2][listID])
end function
function getItemIdx(object item, object listID)
atom i
if sequence(listID) then
listID = getListID(listID)
end if
i = find(item,globalList[2][listID])
return i
end function
procedure main()
atom idx
createList("myCars")
createList("myPhones")
createList("myGirlFriends")
-- one way to add items
addItem("Porsche","myCars")
addItem("123-456-7890","myPhones")
addItem("Jennifer Aniston","myGirlFriends")
-- another way to add items
idx = getListID("myCars") -- assign the atomical index to an atom
addItem("Ferrari",idx) -- could speed things up by reducing a lookup
(getListID()) in addItem()
-- get the index one way
addItem("Porsche","myCars")
idx = getItemIdx("Porsche","myCars")
?idx
-- or another
idx = addItemX("Audi","myCars")
idx = addItemX("99 99 99 99","myPhones")
idx = addItemX("Trinity","myGirlFriends")
pretty_print(1,globalList,"2")
end procedure
main()
?1/0
By doing it this way, I haven't painted myself into a corner
should I want to add other categories (like myHomes, mySoftware,
myVacationSpots, etc.) via an external file or through the
program's interface.
-=ck
"Programming in a state of Euphoria."
http://www.cklester.com/euphoria/
4. Re: Updating parameters in a procedure / function
don cole wrote:
>
> ZNorQ wrote:
> >
> > I want to create a function that adds an element to a sequence, and returns
> > the index in the sequence. I want it to be flexible, so that it can
> > update any sequence - but how do I do that?
> >
> > For example;
> >
> > }}}
<eucode>
> > atom idx idx = 0
> > sequence myCars myCars = {}
> > sequence myPhones myPhones = {}
> > sequence myGirlFriends myGirlFriends = {}
> >
> > function addElement(sequence Element, sequence mySequence)
> > mySequence = append(mySequence, Element)
> > return length(mySequence)
> > end function
> >
> > idx = addElement("Audi", myCars)
> > idx = addElement("99 99 99 99", myPhones)
> > idx = addElement("Trine", myGirlFriends)
> > </eucode>
{{{
> >
> > Regards,
> > Kenneth / ZNorQ
>
> Hello ZNorQ,
>
> It seems to me you would want:
>
> }}}
<eucode>
> atom idx idx = 0
> sequence myCars myCars = {}
> sequence myPhones myPhones = {}
> equence myGirlFriends myGirlFriends = {}
>
> function addElement(sequence Element, sequence mySequence)
> mySequence = append(mySequence, Element)
> return mySequence
> end function
>
> myCars=addElement("Audi",myCars)
> idx=length(myCars)
>
> myPhones = addElement("99 99 99 99", myPhones)
> idx=length(myPhones)
>
> myGirlFriends = addElement("Trine", myGirlFriends)
> idx=length(myGirlFriends)
>
> ----------------also you could-------------------
> function addElement(sequence Element, sequence mySequence)
> mySequence = append(mySequence, Element)
> return {mySequence }& {length(mySequence})
> end function
>
> junk=addElement("Audi",myCars)
> myCars=junk[1]
> idx=junk[2]
>
> junk=addElement("99 99 99 ",myPhones)
> myPhones=junk[1]
> idx=junk[2]
>
> junk=addElement("Trine", GirlFriends)
> myGirlFriends=junk[1]
> idx=junk[2]
>
> > </eucode>
{{{
>
>
> Don Cole
Hey Don,
Yeah, I thought of both those examples, but I was kinda hoping I could do
it in one go only, like in my example... :) I remember back in the pascal
days, you could use a "var" statement (I think) in the procedure/function
declaration, and that would tell it that it should manipulate the global
variable, instead of using a copy of the global. I guess we call them
pointers.. :)
Regards,
Kenneth
5. Re: Updating parameters in a procedure / function
Unfortunally, official RDS Euphoria can't pass parameters by reference.
But Matt's OOEU (in the archieves) allows to do so.
Regards,
Juergen
--
Please excuse my flawed English. My native language is Euphoria.
6. Re: Updating parameters in a procedure / function
Juergen Luethje wrote:
>
> Unfortunally, official RDS Euphoria can't pass parameters by reference.
> But Matt's OOEU (in the archieves) allows to do so.
>
> Regards,
> Juergen
>
> --
> Please excuse my flawed English. My native language is Euphoria.
I see, but would I experience any incompabilities by using his?
Kenneth
7. Re: Updating parameters in a procedure / function
Hi there,
You could also use local variables for the sequences, and pass
the new items name plus a constant to indicate which sequence the
new item should be added too...
constant MYCARS=1,MYTRUCKS=2,MYTHINGS=3 --etc.
sequence MyCars,MyTrucks,MyThings
MyCars={}
MyTrucks={}
MyThings={}
global function AddItem(sequence Item,sequence Type)
if Type=MYCARS then
MyCars=append(MyCars,Item) --or whatever
return length(MyCars)
elsif Type=MYTRUCKS then
MyTrucks=append(MyTrucks,Item) --or whatever
return length(MyTrucks)
elsif Type=MYTHINGS then
MyThings=append(MyThings,Item) --or whatever
return length(MyThings)
else
Print("Unknown Type has been specified")
end if
return 0
end function
AddItem("Benz",MYCARS)
AddItem("Chevy",MYTRUCKS)
AddItem("Coffee Cup",MYTHINGS)
--etc.
Note there are a lot of variations with doing something like this.
The best choice takes into consideration how you need to add
catagories as well as how you need to add items.
This 'local' sequence approach is very clean as the sequences are
encapsulated and they are also relatively small, but to add another
catagory you have to add another
constant and another sequence declaration to the file.
You can also use one big sequence to hold everything, where the
'constants' are generated as you add catagories. You do end up
with more unreadable code however, because you have to use numbers
instead of constants:
AddItem("Chevy",2)
and you have to remember the numbers and which are for what.
Perhaps an associative list where you add catagories:
AddCatagory("MyTrucks")
then you refer to this catagory by name:
AddItem("Chevy","MyTrucks")
You could also use set names to add items:
AddTrucks("Chevy")
but this isnt as flexible.
Good luck,
Al
E boa sorte com sua programacao Euphoria!
My bumper sticker: "I brake for LED's"
From "Black Knight":
"I can live with losing the good fight,
but i can not live without fighting it".
"Well on second thought, maybe not."
8. Re: Updating parameters in a procedure / function
ZNorQ wrote:
>
> etc.
Kenneth:
Why don't you simply use the Euphoria database ?
Bernie
My files in archive:
WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API
Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
9. Re: Updating parameters in a procedure / function
- Posted by Juergen Luethje <j.lue at gmx.de>
Jul 14, 2006
-
Last edited Jul 15, 2006
ZNorQ wrote:
> Juergen Luethje wrote:
>>
>> Unfortunally, official RDS Euphoria can't pass parameters by reference.
>> But Matt's OOEU (in the archieves) allows to do so.
>
> I see, but would I experience any incompabilities by using his?
Most features of OOEU are compatible with RDS Euphoria. But there are
some exceptions, including passing variables by reference to functions
and procedures (for details please see the OOEU documentation).
Using OOEU you can write e.g.:
<ooeucode>
procedure swap (object *x, object *y)
object temp
temp = x
x = y
y = temp
end procedure
integer a, b
a = 1
b = 4
swap(a, b)
? a -- prints 4
? b -- prints 1
</ooeucode>
But running the above code with RDS Euphoria, the first line will cause
an error.
Regards,
Juergen
--
Please excuse my flawed English. My native language is Euphoria.
10. Re: Updating parameters in a procedure / function
ZNorQ wrote:
>
> don cole wrote:
> >
> > ZNorQ wrote:
> > >
> > > I want to create a function that adds an element to a sequence, and
> > > returns
> > > the index in the sequence. I want it to be flexible, so that it can
> > > update any sequence - but how do I do that?
> > >
> > > For example;
> > >
> > > }}}
<eucode>
> > > atom idx idx = 0
> > > sequence myCars myCars = {}
> > > sequence myPhones myPhones = {}
> > > sequence myGirlFriends myGirlFriends = {}
> > >
> > > function addElement(sequence Element, sequence mySequence)
> > > mySequence = append(mySequence, Element)
> > > return length(mySequence)
> > > end function
> > >
> > > idx = addElement("Audi", myCars)
> > > idx = addElement("99 99 99 99", myPhones)
> > > idx = addElement("Trine", myGirlFriends)
> > > </eucode>
{{{
> > >
> > > Regards,
> > > Kenneth / ZNorQ
> >
> > Hello ZNorQ,
> >
> > It seems to me you would want:
> >
> > }}}
<eucode>
> > atom idx idx = 0
> > sequence myCars myCars = {}
> > sequence myPhones myPhones = {}
> > equence myGirlFriends myGirlFriends = {}
> >
> > function addElement(sequence Element, sequence mySequence)
> > mySequence = append(mySequence, Element)
> > return mySequence
> > end function
> >
> > myCars=addElement("Audi",myCars)
> > idx=length(myCars)
> >
> > myPhones = addElement("99 99 99 99", myPhones)
> > idx=length(myPhones)
> >
> > myGirlFriends = addElement("Trine", myGirlFriends)
> > idx=length(myGirlFriends)
> >
> > ----------------also you could-------------------
> > function addElement(sequence Element, sequence mySequence)
> > mySequence = append(mySequence, Element)
> > return {mySequence }& {length(mySequence})
> > end function
> >
> > junk=addElement("Audi",myCars)
> > myCars=junk[1]
> > idx=junk[2]
> >
> > junk=addElement("99 99 99 ",myPhones)
> > myPhones=junk[1]
> > idx=junk[2]
> >
> > junk=addElement("Trine", GirlFriends)
> > myGirlFriends=junk[1]
> > idx=junk[2]
> >
> > > </eucode>
{{{
> >
> >
> > Don Cole
>
> Hey Don,
>
> Yeah, I thought of both those examples, but I was kinda hoping I could do
> it in one go only, like in my example... :) I remember back in the pascal
> days, you could use a "var" statement (I think) in the procedure/function
> declaration, and that would tell it that it should manipulate the global
> variable, instead of using a copy of the global. I guess we call them
> pointers.. :)
>
> Regards,
> Kenneth
Hello ZNorQ ,
As for pointers how about:
I'll just do one database in my example.
function addElement(sequence Element, sequence mySequence)
integer pointer_one,pointer_two
pointer_one=length(mySequence)+1
mySequence = append(mySequence, Element)
pointer_two=length(mySequence)
return {pointer_one,pointer_two)
end function
anns_idx=addElement(myGirlFriends,"Ann")
Just a thought.
Don Cole
11. Re: Updating parameters in a procedure / function
Juergen Luethje wrote:
>
> ZNorQ wrote:
>
> > Juergen Luethje wrote:
> >>
> >> Unfortunally, official RDS Euphoria can't pass parameters by reference.
> >> But Matt's OOEU (in the archieves) allows to do so.
> >
> > I see, but would I experience any incompabilities by using his?
>
> Most features of OOEU are compatible with RDS Euphoria. But there are
> some exceptions, including passing variables by reference to functions
> and procedures (for details please see the OOEU documentation).
> Using OOEU you can write e.g.:
>
> <ooeucode>
> procedure swap (object *x, object *y)
> object temp
> temp = x
> x = y
> y = temp
> end procedure
>
> integer a, b
> a = 1
> b = 4
> swap(a, b)
> ? a -- prints 4
> ? b -- prints 1
> </ooeucode>
>
> But running the above code with RDS Euphoria, the first line will cause
> an error.
>
> Regards,
> Juergen
>
> --
> Please excuse my flawed English. My native language is Euphoria.
Exactly what I wanted. I know there is forces that don't want pointers in
Euphoria (I'm really one of them), but I see that sometimes it come in
handy.
Now, I know this have been discussed before, so please don't start one
again, atleast not with this thread... :)
Kenneth
12. Re: Updating parameters in a procedure / function
Al Getz wrote:
>
> Hi there,
>
> You could also use local variables for the sequences, and pass
> the new items name plus a constant to indicate which sequence the
> new item should be added too...
>
>
> constant MYCARS=1,MYTRUCKS=2,MYTHINGS=3 --etc.
> sequence MyCars,MyTrucks,MyThings
> MyCars={}
> MyTrucks={}
> MyThings={}
>
> global function AddItem(sequence Item,sequence Type)
> if Type=MYCARS then
> MyCars=append(MyCars,Item) --or whatever
> return length(MyCars)
> elsif Type=MYTRUCKS then
> MyTrucks=append(MyTrucks,Item) --or whatever
> return length(MyTrucks)
> elsif Type=MYTHINGS then
> MyThings=append(MyThings,Item) --or whatever
> return length(MyThings)
> else
> Print("Unknown Type has been specified")
> end if
> return 0
> end function
>
> AddItem("Benz",MYCARS)
> AddItem("Chevy",MYTRUCKS)
> AddItem("Coffee Cup",MYTHINGS)
> --etc.
>
> Note there are a lot of variations with doing something like this.
> The best choice takes into consideration how you need to add
> catagories as well as how you need to add items.
> This 'local' sequence approach is very clean as the sequences are
> encapsulated and they are also relatively small, but to add another
> catagory you have to add another
> constant and another sequence declaration to the file.
>
Yes, I did think about doing it this way, and just add another 'elsif'
for each new global sequence I added. I was kinda hoping that this sort
of 'maintenance' would be needless, but I guess this is one of the ways
it could to be done.
> You can also use one big sequence to hold everything, where the
> 'constants' are generated as you add catagories. You do end up
> with more unreadable code however, because you have to use numbers
> instead of constants:
> AddItem("Chevy",2)
> and you have to remember the numbers and which are for what.
> Perhaps an associative list where you add catagories:
> AddCatagory("MyTrucks")
> then you refer to this catagory by name:
> AddItem("Chevy","MyTrucks")
Looks abit like what cklester proposes. Think it's a good way to do it
>
> You could also use set names to add items:
> AddTrucks("Chevy")
>
> but this isnt as flexible.
>
Yeah, that would be a bit too specific for my use at this time, but it
could be a good idea for other uses I guess. If I make some sequence that
is very central in the code, this could be a good way to do it...
>
>
> Good luck,
> Al
>
> E boa sorte com sua programacao Euphoria!
>
>
> My bumper sticker: "I brake for LED's"
>
Thanks for your ideas, Al. :)
Kenneth
13. Re: Updating parameters in a procedure / function
Bernie Ryan wrote:
>
> ZNorQ wrote:
> >
> > etc.
>
> Kenneth:
>
> Why don't you simply use the Euphoria database ?
>
Well, this is really what my program is all about - EuDatabase.
But I'm a bit sceptical since there isn't a built in search function
that does a quick search in the data section - just the key section.
This means that you would have to create quite complex keys for what
fields that the user would need to search in. Please don't answer this
in this thread, since it's off topic. I'm just about to create a new
thread that pin points this...
As for using EuDB instead of sequences, I'm not quite following you.
Wouldn't it be more time consuming to use EuDB instead of sequences
that are stored in memory?
Kenneth.
> Bernie
>
> My files in archive:
> WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API
>
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
14. Re: Updating parameters in a procedure / function
don cole wrote:
>
> ZNorQ wrote:
> >
> > don cole wrote:
> > >
> > > ZNorQ wrote:
> > > >
> > > > I want to create a function that adds an element to a sequence, and
> > > > returns
> > > > the index in the sequence. I want it to be flexible, so that it can
> > > > update any sequence - but how do I do that?
> > > >
> > > > For example;
> > > >
> > > > }}}
<eucode>
> > > > atom idx idx = 0
> > > > sequence myCars myCars = {}
> > > > sequence myPhones myPhones = {}
> > > > sequence myGirlFriends myGirlFriends = {}
> > > >
> > > > function addElement(sequence Element, sequence mySequence)
> > > > mySequence = append(mySequence, Element)
> > > > return length(mySequence)
> > > > end function
> > > >
> > > > idx = addElement("Audi", myCars)
> > > > idx = addElement("99 99 99 99", myPhones)
> > > > idx = addElement("Trine", myGirlFriends)
> > > > </eucode>
{{{
> > > >
> > > > Regards,
> > > > Kenneth / ZNorQ
> > >
> > > Hello ZNorQ,
> > >
> > > It seems to me you would want:
> > >
> > > }}}
<eucode>
> > > atom idx idx = 0
> > > sequence myCars myCars = {}
> > > sequence myPhones myPhones = {}
> > > equence myGirlFriends myGirlFriends = {}
> > >
> > > function addElement(sequence Element, sequence mySequence)
> > > mySequence = append(mySequence, Element)
> > > return mySequence
> > > end function
> > >
> > > myCars=addElement("Audi",myCars)
> > > idx=length(myCars)
> > >
> > > myPhones = addElement("99 99 99 99", myPhones)
> > > idx=length(myPhones)
> > >
> > > myGirlFriends = addElement("Trine", myGirlFriends)
> > > idx=length(myGirlFriends)
> > >
> > > ----------------also you could-------------------
> > > function addElement(sequence Element, sequence mySequence)
> > > mySequence = append(mySequence, Element)
> > > return {mySequence }& {length(mySequence})
> > > end function
> > >
> > > junk=addElement("Audi",myCars)
> > > myCars=junk[1]
> > > idx=junk[2]
> > >
> > > junk=addElement("99 99 99 ",myPhones)
> > > myPhones=junk[1]
> > > idx=junk[2]
> > >
> > > junk=addElement("Trine", GirlFriends)
> > > myGirlFriends=junk[1]
> > > idx=junk[2]
> > >
> > > > </eucode>
{{{
> > >
> > >
> > > Don Cole
> >
> > Hey Don,
> >
> > Yeah, I thought of both those examples, but I was kinda hoping I could do
> > it in one go only, like in my example... :) I remember back in the pascal
> > days, you could use a "var" statement (I think) in the procedure/function
> > declaration, and that would tell it that it should manipulate the global
> > variable, instead of using a copy of the global. I guess we call them
> > pointers.. :)
> >
> > Regards,
> > Kenneth
>
>
> Hello ZNorQ ,
>
> As for pointers how about:
>
> I'll just do one database in my example.
> }}}
<eucode>
> function addElement(sequence Element, sequence mySequence)
> integer pointer_one,pointer_two
> pointer_one=length(mySequence)+1
> mySequence = append(mySequence, Element)
> pointer_two=length(mySequence)
> return {pointer_one,pointer_two)
> end function
>
> anns_idx=addElement(myGirlFriends,"Ann")
>
> </eucode>
{{{
> Just a thought.
> Don Cole
Hey Don,
Not sure if we're talking about the same type of pointers. I was talking
about a pointer to the memory address of the global sequence so that the
function / procdure updates the global instead of creating a local copy
that it manipulates.
I.e.
function addItem(sequence myItem, sequence *mySequence)
.. some code that adds info to mySequence ..
return length(mySequence)
end function
This way I could pass ANY random global sequences that I create, and pass
that into my function, the function would not create a local copy of the
sequence, but instead manipulate the global one. I guess this could save
some memory also, and that would be an idea for very large and complex
programs.
Please forgive me if I misunderstood your suggestion.. :)
PS! My intention is not to create yet another topic about Euphoria and
pointers, so please lets leave it at what suggestions I've had for now..
I think I can utilize those I've already got.. :)
Kenneth