thepip3r
(Hey THIS is FUN)
2005-04-19 05:59 PM
General Question about Using Array Data and how to use their indicies

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?


Bryce
(KiX Supporter)
2005-04-19 06:36 PM
Re: General Question about Using Array Data and how to use their indicies

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



Les
(KiX Master)
2005-04-19 07:07 PM
Re: General Question about Using Array Data and how to use their indicies

Vars in strings are EVIL and array element vars even more so!

thepip3r
(Hey THIS is FUN)
2005-04-19 09:07 PM
Re: General Question about Using Array Data and how to use their indicies

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?


Les
(KiX Master)
2005-04-19 09:23 PM
Re: General Question about Using Array Data and how to use their indicies

ARGH! They are just EVIL! Don't confuse lazy with easy.

thepip3r
(Hey THIS is FUN)
2005-04-19 09:37 PM
Re: General Question about Using Array Data and how to use their indicies

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?

Les
(KiX Master)
2005-04-19 09:55 PM
Re: General Question about Using Array Data and how to use their indicies

Easy!
'string'+$Var+'another string'+$AnotherVar+'a string with "double quotes" in it'+$YetAnotherVar
and so on...


Les
(KiX Master)
2005-04-19 10:01 PM
Re: General Question about Using Array Data and how to use their indicies

Lazy!
'string $Var another string $AnotherVar a string with "double quotes" in it $YetAnotherVar'
I almosed puked just typing this out.


thepip3r
(Hey THIS is FUN)
2005-04-19 10:01 PM
Re: General Question about Using Array Data and how to use their indicies

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?


Les
(KiX Master)
2005-04-19 10:06 PM
Re: General Question about Using Array Data and how to use their indicies

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.

thepip3r
(Hey THIS is FUN)
2005-04-19 10:22 PM
Re: General Question about Using Array Data and how to use their indicies

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...

Bryce
(KiX Supporter)
2005-04-19 10:41 PM
Re: General Question about Using Array Data and how to use their indicies

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


thepip3r
(Hey THIS is FUN)
2005-04-19 10:53 PM
Re: General Question about Using Array Data and how to use their indicies

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.

Richard H.Administrator
(KiX Supporter)
2005-04-20 10:58 AM
Re: General Question about Using Array Data and how to use their indicies

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?


Les
(KiX Master)
2005-04-20 03:54 PM
Re: General Question about Using Array Data and how to use their indicies

here is another example:

$varone='One'
$vartwo='Two'
$varthree='Three'
$String='CanYouPut$varoneInTheStringAnd$vartwoAsWellWithoutSomeSpaceDelimiter?'
$String ?


thepip3r
(Hey THIS is FUN)
2005-04-20 05:22 PM
Re: General Question about Using Array Data and how to use their indicies

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?


ShawnAdministrator
(KiX Supporter)
2005-04-20 05:35 PM
Re: General Question about Using Array Data and how to use their indicies

You tell'em pip3r, I've never had any problems with vars in strings either. :0)

Les
(KiX Master)
2005-04-20 05:42 PM
Re: General Question about Using Array Data and how to use their indicies

I don't understand the Q.
You are mixing singles and doubles, effectively embedding quotes.

$String="CanYouPut"+$varone+"InTheStringAnd"+$vartwo+"AsWellWithoutSomeSpaceDelimiter?"


Les
(KiX Master)
2005-04-20 05:44 PM
Re: General Question about Using Array Data and how to use their indicies

Shawn,
GTH


Bryce
(KiX Supporter)
2005-04-20 05:49 PM
Re: General Question about Using Array Data and how to use their indicies

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





Richard H.Administrator
(KiX Supporter)
2005-04-20 06:44 PM
Re: General Question about Using Array Data and how to use their indicies

Quote:

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.




Aye, and there's the rub.

It's fine while it works - when it doesn't work its a real problem and it may take you a while to discover why your previously well behaved program has stopped working.

You don't need to set NoVarsInStrings. Just don't use vars in strings.

It's just bad coding practice. It doesn't mean your code won't work just as well as code which doesn't have vars in strings. However you are more likely to encounter a situation where it will fail, and even worse is that you are less likely to know about it.

You're probably fed up with examples by now, but here is a good example of code not working as you'd expect:
Code:
If "$VAR" = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf
If $VAR = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf



The two conditionals are identical, except one has the variable in a string.

The output from this is:
Code:
Variable $VAR is set
Variable $VAR is not set



Now, that first result can't possibly be what the coder intended, can it?


thepip3r
(Hey THIS is FUN)
2005-04-20 07:02 PM
Re: General Question about Using Array Data and how to use their indicies

No I don't think it would be their intention but my latest question still hasn't been answered: You don't have to use vars between quotes even when NoVarsInStrings is off do you? i.e.

even when I use vars in strings, if I'm testing to see if a var is set or not, I'd use your second example over the first. The only time I actually use vars in strings is when I'm 'echoing' a string and have a var in it. I mean, in your first example (if you can even do it), why would you use quotes when you don't need it?


Richard H.Administrator
(KiX Supporter)
2005-04-20 07:48 PM
Re: General Question about Using Array Data and how to use their indicies

Quote:

You don't have to use vars between quotes even when NoVarsInStrings is off do you?



No you don't have to.

Whether NoVarsInStrings is on or off, don't put vars in strings. You do need to be aware of the setting of NoVarsInStrings if you are using the "$" character in a string as a real character rather than as a variable metacharacter.

Quote:

I mean, in your first example (if you can even do it), why would you use quotes when you don't need it?




I did do it. That's real code and real results cut'n'pasted from my own console.

The example illustrates the mechanism by which even simple code can get screwed up, I'm sure you can extrapolate the example to some more realistic scenarios.


thepip3r
(Hey THIS is FUN)
2005-04-20 08:01 PM
Re: General Question about Using Array Data and how to use their indicies

I do understand what you're saying but in all examples, I'd be able to escape out of the quotes to get the result I'm looking for. I guess it's just going to take what you said Richard. Thanx for trying to steer me clear of going headlong off a cliff but I usually end up doing things the hard way first.

=P


Sealeopard
(KiX Master)
2005-04-24 10:01 PM
Re: General Question about Using Array Data and how to use their indicies

Then maybe this will help. the option to have vars inside strings beign procesed can be deprecated int he future. I remember Ruud making such a statement some time ago. Also, by using the NoVarsInStrings=ON option one does nto have to worry about the regular '$' sign charatcer in strings. With NoVarsInStrings=ON a single $-sign is sufficient, which comes in very handy if you start using the EXECUTE() function or KiXforms.

So, take some advise for the old guys, and try to code cleaner, life will be easier in the long run.