#138178 - 2005-04-19 05:59 PM
General Question about Using Array Data and how to use their indicies
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
if $PLIST is an array that has all processes running on a machine:
Code:
$PList = QS(Split(ListProcess('.'),@CRLF))
and I try to call each array value by it's index, why do I have to do this:
Code:
For Each $proc In $PList $processSQL = "INSERT INTO processes(cid,Name,ProcessID,ProcessPath) VALUES('$cid','"+Split($proc,"*")[0]+"','"+Split($proc,"*")[1]+"','"+Split($proc,"*")[2]+"')" $ = DBExecuteSQL($objConn,$processSQL) ? "Error @ERROR: @SERROR" Next
Instead of just being able to use this?:
Code:
For Each $proc In $PList $processSQL = "INSERT INTO processes(cid,Name,ProcessID,ProcessPath) VALUES('$cid','"+$proc[0]+"','"+$proc[1]+"','"+$proc[2]+"')" $ = DBExecuteSQL($objConn,$processSQL) ? "Error @ERROR: @SERROR" Next
I just don't see what the split() is doing that it's presence not being in my second code is causing it not to work. Also, this does not write to my database, when it tries to write the processes, the first error says, "Error 87: The parameter was incorrect" and then it says, "The Operation Completed Successfully" on the rest only no information is written to the DB. Can anyone help me on either issue?
|
|
Top
|
|
|
|
#138179 - 2005-04-19 06:36 PM
Re: General Question about Using Array Data and how to use their indicies
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
my comments are inline....
Code:
For Each $proc In $PList
;this will turn the string value of $proc into an array. $proc = split($proc,'*')
$processSQL = "INSERT INTO processes(cid,Name,ProcessID,ProcessPath)
; i have no idea what is going on here.... but one thing that i see is, ; you can not put array index insode of a string. hell you should not be putting ; var's in a string at all... your quoptes are all ooer the road. ; you even have a quote on the outside of the ending )
VALUES('$cid','"+$proc[0]+"','"+$proc[1]+"','"+$proc[2]+"')"
;since i have no idea what the VALUES UDF is doing.... looks like you are wanting to ; create a string using the array indexes and putthem into a comma delimited string.
VALUES($cid,"'" + $proc[0] + "','" + $proc[1] + "','" + $proc[2] +"'")
$ = DBExecuteSQL($objConn,$processSQL)
? "Error @ERROR: @SERROR" Next
|
|
Top
|
|
|
|
#138181 - 2005-04-19 09:07 PM
Re: General Question about Using Array Data and how to use their indicies
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
Ok, here's how I got it to work with the array and it'd indicies:
Code:
For Each $proc In $PList $proc1 = Split($proc,"*")[0] $proc2 = Split($proc,"*")[1] $proc3 = Split($proc,"*")[2]
$processSQL = "INSERT INTO processes(cid,Name,ProcessID,ProcessPath) VALUES('$cid','$proc1','$proc2','$proc3')" $ = DBExecuteSQL($objConn,$processSQL) ? "Error @ERROR: @SERROR" Next
Bryce: Thank you for the suggestions but they were a little off. Values is not a UDF, it's a MySQL/SQL command. It's apart of the $processSQL line. I was just asked on my last post to break up my SQL statements with a return character so that the code didn't break this forums tables out to more than 2500 pixels again. And my quotes aren't all over the road, if KiX supported array elements in strings, this is probably exactly how it would look. There are just a lot of quotes because I'm stepping out of the text portion of the variable and using a fuction and concatenating them together inside the string.
Bryce/Les: What is so evil about using variables in strings? It's great and it makes coding so much easier. Is there a security aspect I'm missing or is it just your coding preference?
Edited by thepip3r (2005-04-19 09:08 PM)
|
|
Top
|
|
|
|
#138186 - 2005-04-19 10:01 PM
Re: General Question about Using Array Data and how to use their indicies
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
But what is the purpose of that? Does it offer faster processing or does having vars in strings pose a vulnerability that I don't know about?
To me, this:
Code:
$string = "The color of the ball is $color."
is easier and much more efficient than:
Code:
$string = "The color of the ball is " + $color + "."
Again, is it simply preference or is there some benefit to not using them inside of the string?
|
|
Top
|
|
|
|
#138189 - 2005-04-19 10:41 PM
Re: General Question about Using Array Data and how to use their indicies
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Using a variable inside a string is valid kix code, but it can lead to undesired effects, especially when you are trying to use $, @ and % in your strings. And when you are using the execute() command, things can get weird...
basically the solution you found is defining $proc1, $proc2, and $proc3 as string variables, not array elements. Since they are string variables you can put them inline of a string as long as setoptions('novarsinstring') is set to off, this is kix's default setting.
And last... it comes down to personal preference
|
|
Top
|
|
|
|
#138191 - 2005-04-20 10:58 AM
Re: General Question about Using Array Data and how to use their indicies
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is a vary simple example of why vars in strings should be avoided: Code:
"VAR is now '$VAR'"+@CRLF $VAR="" "VAR is now '$VAR'"+@CRLF
Results are: Code:
VAR is now '$VAR' VAR is now ''
Pretty nasty, I'm sure you'll agree.
If you use variables in strings you cannot be sure of the action - it is dependant on the context of the string, and no-one is a fan of ambiguous process when you are trying to write a program.
If a variable is in a string any typo in the variable name cannot be caught as an undeclared variable. You are using SetOption("Explicit","ON"), aren't you?
|
|
Top
|
|
|
|
#138193 - 2005-04-20 05:22 PM
Re: General Question about Using Array Data and how to use their indicies
|
thepip3r
Hey THIS is FUN
   
Registered: 2005-03-02
Posts: 350
|
Richard -- I guess I've never had a problem using vars in strings before which is why I don't see why everyone is so against it.
Les -- You're right, I didn't think of that example. I suppose the vars in $String would be terribly misinterpreted with VarsInStrings On right? Well, is there a way even with VarsInStrings to step out of the text? i.e.
Code:
$varone='One' $vartwo='Two' $varthree='Three' $String='CanYouPut"+$varone+"InTheStringAnd"+$vartwo+"AsWellWithoutSomeSpaceDelimiter?' $String ?
would that work?
Edited by thepip3r (2005-04-20 05:22 PM)
|
|
Top
|
|
|
|
#138197 - 2005-04-20 05:49 PM
Re: General Question about Using Array Data and how to use their indicies
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
ok i meant to comment on this yesterday... but work pulled me away.
I was noticing that basically you have a string that is build like this. "item1*item2*item3" and you want to turn it into a comma delimited string with quotes like this "item1","item2","item3"
Code:
$data = "item1*item2*item3" $data = '"'+Join(Split($data,'*'),'","') + '"'
? $data
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 874 anonymous users online.
|
|
|