Page 1 of 1 1
Topic Options
#36718 - 2003-02-20 10:15 AM Combine Variable
ladybird Offline
Fresh Scripter

Registered: 2002-08-11
Posts: 19
Really need help on this.

I would like to combine 2 variables into 1 new variable.

Example:
$total = 3
$Cnt = 0
$var1 = string1
$var2 = string2
$var3 = string3

While $total > $Cnt
$Cnt = $Cnt + 1
$newvar = $var + $Cnt <------
? $newvar
Loop

What i would like to have: $newvar should have result as $var1 where is "string1".

Actually, i would like to loop as many as $total variable defined and use $var1, $var2, $var3 when is looping.

Thanks in advance.

Top
#36719 - 2003-02-20 10:18 AM Re: Combine Variable
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
ummm ... didn't get that completely .. just drunk my 3rd Cup of Coffee [Wink]

well, if $total is constant you could get rid of the while thing and do



for $i=0 to $total 
    ;here is the part i didn't understood
next



More Info ?
_________________________



Top
#36720 - 2003-02-20 10:23 AM Re: Combine Variable
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Ah ... hmmmm .... just patch strings together ?

How about building an array of strings and patch them together with the JOIN() function ?

silly sample :



break on

dim $a[2]

$a[0] = "this is the "
$a[1] = "result of "
$a[2] = "join() ..."

$patch = join($a)

$patch

get $



hth
J.
_________________________



Top
#36721 - 2003-02-20 10:41 AM Re: Combine Variable
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It took me a couple of minutes to get my head around your post, but I think this is what you want:
code:
$iTotal=3
$sVar1="String 1"
$sVar2="String 2"
$sVar3="String 3"

For $iCount = 1 To $iTotal
$sNull=Execute("$$sNewvar=$$sVar"+$iCount)
"$$sNewvar="$sNewvar ?
Next


Top
#36722 - 2003-02-20 10:52 AM Re: Combine Variable
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
But WHY would one change a variable in a loop, no better question : Why would one do it this way ?

Please explain Ladybird [Confused]
_________________________



Top
#36723 - 2003-02-20 11:09 AM Re: Combine Variable
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Here is a slightly different version that doesn't require you to set the "total" variable:

$sVar1="String 1"
$sVar2="String 2"
$sVar3="String 3"

$iCount=1
While Execute("Exit VarType($$sVar"+$iCount+")")
$sNull=Execute("$$sNewvar=$$sVar"+$iCount)
"$$sNewvar="$sNewvar ?
$iCount=$iCount+1
Loop

Top
#36724 - 2003-02-20 11:18 AM Re: Combine Variable
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Ladybird ,

you say :

quote:
I would like to combine 2 variables into 1 new variable.

but you defined 3

later on there is contradiction :

quote:
What i would like to have: $newvar should have result as $var1 where is "string1".
Please !
In order to help us help you you should really explain that a bit closer [Wink]
_________________________



Top
#36725 - 2003-02-20 04:58 PM Re: Combine Variable
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Also, why do you want to dynamically create a lot of new variable names?

Or do you want ot access arrays?

I think we're all a little bit confused as what exactly you try to do.
_________________________
There are two types of vessels, submarines and targets.

Top
#36726 - 2003-02-21 02:17 AM Re: Combine Variable
ladybird Offline
Fresh Scripter

Registered: 2002-08-11
Posts: 19
Richard has answered my questions. Perfect!!

I just change the variable rather to change the code when everytime the requirement is changed.

Thank You!!

Top
#36727 - 2003-02-21 02:20 AM Re: Combine Variable
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Could you please explain that? I mean what exactly do yo try to accomplish with this convoluted task? I don't really see usefulness in it [Confused]
_________________________
There are two types of vessels, submarines and targets.

Top
#36728 - 2003-02-21 04:20 AM Re: Combine Variable
ladybird Offline
Fresh Scripter

Registered: 2002-08-11
Posts: 19
The reason:

I just change the variable rather to change the code/logic when everytime the requirement is changed.

Top
#36729 - 2003-02-21 04:39 AM Re: Combine Variable
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Sorry, but you've lost me on that one.
_________________________
There are two types of vessels, submarines and targets.

Top
#36730 - 2003-02-22 07:59 PM Re: Combine Variable
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Now Now ...

changing a variable is better practice than changing code, that is already defined by the name 'variable' ... LOL !

But ... Ladybird If you would have shown us the code or at least a snippet of it we would have understood what you tried to accomplish (Plus, in most of the cases I saw there was a great improvement provided by the Kixpharts [Wink] )

Let us see, pleeease [Big Grin]

J.
_________________________



Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 957 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.064 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org