Re: A better way to do this?
- Posted by Alexander Toresson <alexander.toresson at gmail.com> Feb 26, 2005
- 446 views
Brent W. Hughes wrote: > Consider the following code which does not do what I want: > --**************************************** > procedure Sub2(sequence MyList) > MyList[900] = 5 > end procedure > ----------------------------------------- > procedure Sub1() > sequence List > List = repeat(0,10000) > Sub2(List) > -- Here, I would like List[900] to be 5. > -- But, of course, it's not; it's 0. > end procedure > ------------------------------------------ > Sub1() > --**************************************** > I really wanted Sub2 to change the 900th element of List. > Here are a couple of solutions that I don't like: > --**************************************** > function Sub2(sequence MyList) > MyList[900] = 5 > return MyList > end function > ----------------------------------------- > procedure Sub1() > sequence List > List = repeat(0,10000) > List = Sub2(List) > end procedure > ------------------------------------------ > Sub1() > --**************************************** > This has to copy 10000 elements to make one little change. > Here's the other solution: > --**************************************** > sequence List > ------------------------------------------ > procedure Sub2() > List[900] = 5 > end procedure > ------------------------------------------ > procedure Sub1() > List = repeat(0,10000) > Sub2() > end procedure > ------------------------------------------ > Sub1() > --**************************************** > I don't really like giving List such a wide scope in this way. > > Is there another, more elegant, solution to this problem? > Unfortunately, no. I've also noticed the problem. If you could do pass-by-reference in euphoria, this would have been solved. I used the same solution as you in an app of mine, however, I had 6 big sequences. This means I had to duplicate the *same* code 6 times, and I had to fix bugs 6 times, add features 6 times etc, which was *very* unelegant. However, this sped up the program by at least 100x. So if pass-by-reference would be implemented, we'd get this advantage without this ugly hack. Regards, Alexander Toresson