Page 1 of 1 1
Topic Options
#201543 - 2011-02-09 12:32 PM Array De-Dupe Problem
GeorgeLittle Offline
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 ?

 Code:
: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 \:D

Top
#201544 - 2011-02-09 01:54 PM Re: Array De-Dupe Problem [Re: GeorgeLittle]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Search the UDF forum, you'll find hundreds of functions contributed by board members that will assist for these sort of tasks.

For example Qsort() is a fast array sorter (much faster than a bubble sort) and RemoveDups() is just one of a number of UDFs for removing duplicates.

Spend a little time digging around in the UDF forum and you'll find the odd golden nugget.

Top
#201545 - 2011-02-09 01:56 PM Re: Array De-Dupe Problem [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
By the way, what version of KiXtart are you using?

We rarely see a sub-routine any more, they are just soooo last-decade.

Almost forgot - Welcome to the board!

Top
#201619 - 2011-02-18 03:48 PM Re: Array De-Dupe Problem [Re: GeorgeLittle]
GeorgeLittle Offline
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 Offline
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:
 Code:
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


 Quote:
D:\temp>kix32 uniq.kix
aaa bbb ccc ddd
aaa

ERROR : array reference out of bounds!
Script: D:\temp\uniq.kix
Line : 37

Top
#201623 - 2011-02-20 02:24 AM Re: Array De-Dupe Problem [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
That version of Uniq() is pretty old.. Version 1.1 is available on my web site's Kixtart UDF Library and includes an error-check to avoid this situation.

All of the UDFs that we use in production code are published nightly from our active UDF library to our web site - it's the best place to get the most current UDFs that we've written.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#201624 - 2011-02-21 01:17 PM Re: Array De-Dupe Problem [Re: Glenn Barnas]
GeorgeLittle Offline
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

 Code:
; 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 Offline
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 Administrator Offline
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! \:D

Top
#201627 - 2011-02-21 02:49 PM Re: Array De-Dupe Problem [Re: Glenn Barnas]
GeorgeLittle Offline
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 Offline
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
Page 1 of 1 1


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

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.071 seconds in which 0.032 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