#201543 - 2011-02-09 12:32 PM
Array De-Dupe Problem
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Hi Guys,
I have a script that queries group membership and returns a 4 digit number. The above routine sorts this into order. What I would like it to do is remove any duplicates ?
:Sort_Branches
Do
$r = 0
$changes = 0
While $r < ($br_count - 1)
If ($TS_Branches_Im_in[$r] > $TS_Branches_Im_in[$r+1])
$temp = $TS_Branches_Im_in[$r+1]
$TS_Branches_Im_in[$r+1] = $TS_Branches_Im_in[$r]
$TS_Branches_Im_in[$r] = $temp
$changes = 1
EndIf
$r = $r + 1
Loop
Until $changes = 0
Return
Thanks guys a distressed sys admin
|
|
Top
|
|
|
|
#201619 - 2011-02-18 03:48 PM
Re: Array De-Dupe Problem
[Re: GeorgeLittle]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
I used the Unique udf but that now comes up with an error about my array being out of bounds
|
|
Top
|
|
|
|
#201622 - 2011-02-19 11:26 PM
Re: Array De-Dupe Problem
[Re: GeorgeLittle]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Could be a number of reasons, but Uniq() (if that's the UDF that you are using) doesn't sanity check the input and will generate an out of bounds error if you don't pass an array.
In this example the first call correctly removed the duplicate "ccc" entry, the second call handles the single element array OK, but the third call which has a simple strin causes an error:
Join(Uniq(Split("aaa bbb ccc ccc ddd")))+@CRLF
Join(Uniq(SPlit("aaa")))+@CRLF
Join(Uniq("aaa"))+@CRLF
;;
;;======================================================================
;;
;;FUNCTION uniq()
;;
;;ACTION Removes duplicates from a sorted, 1-dimension array
;;
;;AUTHOR Glenn Barnas / FRIT-EROC
;;
;;SYNTAX uniq(array)
;;
;;PARAMETERS array - array to remove duplicates from
;;
;;REMARKS Array must be sorted first - no check for sort is performed
;;
;;RETURNS array with duplicate items removed
;;
;;DEPENDENCIES none
;;
;;TESTED WITH NT4, W2K, WXP
;;
;;EXAMPLES $Singles = Uniq($has_dups)
;
Function Uniq($A)
; $TOP is last entry, $X is source pointer, $Y is destination pointer
Dim $Top, $X, $Y
$Top = UBound($A) ; Find size of original array
Dim $WA[$Top] ; Create working array
$Y = 1 ; output array pointer
$WA[0] = CStr($A[0]) ; copy first element
For $X = 1 to $Top
If $WA[$Y - 1] <> $A[$X] ; is current value different from last?
$WA[$Y] = CStr($A[$X]) ; add it to output
$Y = $Y + 1 ; increment output pointer
EndIf
Next
ReDim Preserve $WA[$Y - 1] ; resize output array
$Uniq = $WA ; return the array of unique elements
EndFunction
D:\temp>kix32 uniq.kix aaa bbb ccc ddd aaa
ERROR : array reference out of bounds! Script: D:\temp\uniq.kix Line : 37
|
|
Top
|
|
|
|
#201624 - 2011-02-21 01:17 PM
Re: Array De-Dupe Problem
[Re: Glenn Barnas]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Hi,
Thanks for this. I have now updated the array the piont I get the array out of bounds is in the section of code here
; Create Screen Master Design
Box(1, 20, 5, 60, double)
At (2,21) " Please Select the Branch that you "
At (3,21) " would like to connect to, from the"
At (4,21) " list below..."
At (7, 1) "BRANCHES AVAILABLE:"
Box (8, 1, 18, 77, single)
At (20, 1) "Use UP, DOWN, LEFT, RIGHT keys to Select Branch Number, then Press RETURN"
Do
;Clear Main Box
Color W+/B+
$z = 9
While $z < 18
At($z,3) " "
$z = $z + 1
Loop
;Write to Main Box
$w = 0
$br_x = 3
$br_y = 9
While $w < $br_count
If($br_y = $y) And ($br_x = $x)
;Write Cursor
Color Y+/R+
At ($br_y, $br_x) $TS_Branches_Im_in[$w]
Color W+/B+
$Branch = $TS_Branches_Im_in[$w]
Else
At ($br_y, $br_x) $TS_Branches_Im_in[$w]
EndIf
$w = $w + 1
If $w < $br_count
$br_y = $br_y + 2
If ($br_y > 17)
$br_x = $br_x + 6
$br_y = 9
EndIf
EndIf
Loop
;Move Cursor
Get $input1
;Check if RETURN has been pressed
If $input1 = Chr(13)
Goto End_Of_Section
EndIf
;get Movement of cursor
While $input1 <> à
Get $input1
Loop
Get $input2
$last_y = $y
$last_x = $x
Select
Case ($input1 = à) And ($input2 = H) ;UP
If $y > 9
$y = $y - 2
EndIf
Case ($input1 = à) And ($input2 = P) ;DOWN
If $y < 17
$y = $y + 2
EndIf
Case ($input1 = à) And ($input2 = M) ;RIGHT
If $x < 69
$x = $x + 6
EndIf
Case ($input1 = à) And ($input2 = K) ;LEFT
If $x > 3
$x = $x - 6
EndIf
EndSelect
Select
Case $x = $br_x And $y > $br_y
$y =$last_y
$x =$last_x
Case $x > $br_x
$y =$last_y
$x =$last_x
EndSelect
Until $input1 = ""
Specifcally at the EndIf statement with the array out of bounds error
|
|
Top
|
|
|
|
#201625 - 2011-02-21 01:22 PM
Re: Array De-Dupe Problem
[Re: GeorgeLittle]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Oh and sorry I really just dont get kix
|
|
Top
|
|
|
|
#201626 - 2011-02-21 02:11 PM
Re: Array De-Dupe Problem
[Re: GeorgeLittle]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
When I run this snippet, I get no error, but no display, either. When I define the $TS_Branches_Im_in with a couple of names, I get a display, but the cursor doesn't move. When I hit Enter, I get an error saying label not found. Good thing, since the next block of code is an infinite loop!
I also had to set the value of $br_count to the size of the array using UBound.
Where are the initial values of $X and $Y defined? These are compared, but are uninitialized so are zero.
The input logic is odd.. Think about an input loop with all the conditions - UP, DOWN, LEFT, RIGHT, and ENTER.. with ENTER exiting the loop and obtaining the selection.
Cursor moves may seem fancy, but users will probably be bothered by all this motion when you can simply number the selections and prompt for the selection ID.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#201627 - 2011-02-21 02:49 PM
Re: Array De-Dupe Problem
[Re: Glenn Barnas]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
The input logic is totally odd
No values exist for $X and $Y
It the Loop for the Write main box at the first EndIF that causes the problem. The branches are numbers.
|
|
Top
|
|
|
|
#201628 - 2011-02-21 03:14 PM
Re: Array De-Dupe Problem
[Re: GeorgeLittle]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Glen,
Thanks for that a ubound in the right place did the the trick.
Your a star.
!!!!!!!
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|