Page 1 of 2 12>
Topic Options
#138178 - 2005-04-19 05:59 PM General Question about Using Array Data and how to use their indicies
thepip3r Offline
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 Offline
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
#138180 - 2005-04-19 07:07 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Vars in strings are EVIL and array element vars even more so!
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138181 - 2005-04-19 09:07 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
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
#138182 - 2005-04-19 09:23 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
ARGH! They are just EVIL! Don't confuse lazy with easy.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138183 - 2005-04-19 09:37 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
I don't even understand how you would insert the data into the string without using variables. Please keep in mind that my only coding experience is in PHP and it's a much more open and easy format to write to. Can you explain to me how using vars in strings is lazy as opposed to easy so I can know what you're talking about?
Top
#138184 - 2005-04-19 09:55 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Easy!
'string'+$Var+'another string'+$AnotherVar+'a string with "double quotes" in it'+$YetAnotherVar
and so on...
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138185 - 2005-04-19 10:01 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Lazy!
'string $Var another string $AnotherVar a string with "double quotes" in it $YetAnotherVar'
I almosed puked just typing this out.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138186 - 2005-04-19 10:01 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
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
#138187 - 2005-04-19 10:06 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Not a security vulnerability but a vulnerability to get bit on the ass. You were alreay bit once with your array elements but you still did not learn your lesson.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138188 - 2005-04-19 10:22 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
How was I bit on the ass? All I did was call the array element incorrectly according to KiX. That wasn't a bite on the ass, that was a coding mistake and if I'm correct, this is the "Starters" category. If there's "Noob" category that I missed, please direct me to the correct area to post. I just wanted a simple answer, preference or problem. And it appears the only reason not to use vars in strings (based off of what you've said), it's only preference...
Top
#138189 - 2005-04-19 10:41 PM Re: General Question about Using Array Data and how to use their indicies
Bryce Offline
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
#138190 - 2005-04-19 10:53 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
Thank you Bryce, side-effects/security problems/etc was what I was looking for in regards to problems with using them. Thanx to both of you for the help on my problem.
Top
#138191 - 2005-04-20 10:58 AM Re: General Question about Using Array Data and how to use their indicies
Richard H. Administrator Offline
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
#138192 - 2005-04-20 03:54 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
here is another example:

$varone='One'
$vartwo='Two'
$varthree='Three'
$String='CanYouPut$varoneInTheStringAnd$vartwoAsWellWithoutSomeSpaceDelimiter?'
$String ?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138193 - 2005-04-20 05:22 PM Re: General Question about Using Array Data and how to use their indicies
thepip3r Offline
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
#138194 - 2005-04-20 05:35 PM Re: General Question about Using Array Data and how to use their indicies
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You tell'em pip3r, I've never had any problems with vars in strings either. :0)
Top
#138195 - 2005-04-20 05:42 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I don't understand the Q.
You are mixing singles and doubles, effectively embedding quotes.

$String="CanYouPut"+$varone+"InTheStringAnd"+$vartwo+"AsWellWithoutSomeSpaceDelimiter?"
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138196 - 2005-04-20 05:44 PM Re: General Question about Using Array Data and how to use their indicies
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Shawn,
GTH
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138197 - 2005-04-20 05:49 PM Re: General Question about Using Array Data and how to use their indicies
Bryce Offline
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
Page 1 of 2 12>


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

Who's Online
0 registered and 874 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.127 seconds in which 0.058 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