Page 1 of 1 1
Topic Options
#208409 - 2014-01-27 07:52 PM Bug with array
ChristopheM Offline
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 :
 Code:
"---------------------------" ?
" 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 :
 Code:
"---------------------------" ?
" 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
#208410 - 2014-01-27 08:09 PM Re: Bug with array [Re: ChristopheM]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
The only difference between your code segments is one causes the script to abort, and the other doesn't. However, if you put @serror after your illegal array element, you get: "The storage control block address is invalid."

I'm not sure what you are hoping for, but referencing elements outside the bounds of the array is bad either way.
_________________________
(... better days ahead)

Top
#208411 - 2014-01-27 08:10 PM Re: Bug with array [Re: ChristopheM]
Glenn Barnas Administrator Offline
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:
 Code:
$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! \:D

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 (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.057 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

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