#208409 - 2014-01-27 07:52 PM
Bug with array
|
ChristopheM
Hey THIS is FUN
   
Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
|
recently, i checked a script and founded not optimized code. I decided to make little changes and script started to fail. Here is an example :"---------------------------" ?
" kix version : " @KIX ?
"---------------------------" ?
dim $arr, $t1, $t2, $t3, $t4, $t5
$line = "field 0;field 1;field 2;field 3"
"- not good programming but works" ?
$t1 = split( $line, ";" )[0]
$t2 = split( $line, ";" )[1]
$t3 = split( $line, ";" )[2]
$t4 = split( $line, ";" )[3]
$t5 = split( $line, ";" )[4]
"- good programming but doesn't work !!!" ?
$arr = split( $line, ";" )
$t1 = $arr[0]
$t2 = $arr[1]
$t3 = $arr[2]
$t4 = $arr[3]
$t5 = $arr[4] First part is OK, second part generates an error "ERROR : array reference out of bounds!"
I decide to test a new version :"---------------------------" ?
" kix version : " @KIX ?
"---------------------------" ?
dim $arr, $t1, $t2, $t3, $t4, $t5
$line = "field 0;field 1;field 2;field 3"
"- not good programming but works" ?
$t1 = mysplit( $line, ";" )[0]
$t2 = mysplit( $line, ";" )[1]
$t3 = mysplit( $line, ";" )[2]
$t4 = mysplit( $line, ";" )[3]
$t5 = mysplit( $line, ";" )[4]
"- good programming but doesn't work !!!" ?
$arr = split( $line, ";" )
$t1 = $arr[0]
$t2 = $arr[1]
$t3 = $arr[2]
$t4 = $arr[3]
$t5 = $arr[4]
function mysplit( $str, $sep )
$mysplit = split( $str, $sep )
endfunction Exactly the same problem. Wrong code is working and "good" code is not working (good code should test index with UBound)
I just write kix version at the begining because i test with 4.53 and 4.60 to 4.64. All versions have the same problem.
Ruud,
this is dangerous to change something now because many scripts could fail suddenly but have you already seen this problem ?
_________________________
Christophe
|
|
Top
|
|
|
|
#208411 - 2014-01-27 08:10 PM
Re: Bug with array
[Re: ChristopheM]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
|
The second part of the code is working as expected - you fill an array from Split, and it has a specific number of elements (as can be shown by UBound, and would be 3 in this case). If you ask for element 4 in a 3-element array, you'll get the above error.
The Split(X, d)[n] format performs a split, and the [n] asks for the n'th element. You aren't directly accessing the array, so the out of bounds error doesn't display. It probably should, though. As such, instead of an error, you get an empty value. You're right - this is bad coding! 
If you expect 5 elements returned from a split, try this:$line = "field 0;field 1;field 2;field 3"
$aTmp = Split($Line + ';;;;', ';') ; insure 5 elements in array
$t1 = $aTmp [0]
$t2 = $aTmp [1]
$t3 = $aTmp [2]
$t4 = $aTmp [3]
$t5 = $aTmp [4] Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Henriques)
and 2516 anonymous users online.
|
|
|