#163386 - 2006-06-17 01:59 AM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Code:
$previousstate = SetOption( "NoVarsInStrings", "ON" )
dim $A[9] $A[0] = 1 $A[1] = 7 $A[2] = 3 $A[3] = 4 $A[4] = 5 $A[5] = 5 $A[6] = 7 $A[7] = 2 $A[8] = 1 $A[9] = 1
$sorted = qs($A)
for each $x in $sorted ? $x next ? for each $x in GetFirstFiveUnique($A) ? $x next
function GetFirstFiveUnique($Array) dim $i, $x, $y, $sorted, $Result[0]
$sorted = qs($Array) $x = 0 for $i=0 to ubound($sorted)
if $i=0 $Result[0] = $sorted[0] $x = 1 + $x endif if $i > 0 if $Result[$x-1] <> $sorted[$i] Redim Preserve $Result[$x] $Result[$x] = $sorted[$i] $x = 1 + $x endif endif if $x = 5 $i = 1 + ubound($Array) endif next $GetFirstFiveUnique = $Result
endfunction
function qs($a) DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l $b[0]=0 $c[0]=UBOUND($a) $d=0 While $d >=0 $e=$b[$d] $f=$c[$d] While $e < $f $h=$e+($f-$e)/2 $k=$a[$e] $A[$e]=$A[$h] $A[$h]=$k $i=$e+1 $j=$f $l=0 Do While ($i<$j) AND $A[$e] > $A[$i] $i=$i+1 Loop While ($j>=$i) AND $A[$j] > $A[$e] $j=$j-1 Loop IF $i>=$j $l=1 ELSE $k=$A[$i] $A[$i]=$A[$j] $A[$j]=$k $j=$j-1 $i=$i+1 ENDIF Until $l=1 $k=$a[$e] $a[$e]=$a[$j] $a[$j]=$k $g=$j If $g-$e <= $f - $g If $g+1 < $f $b[$d]=$g+1 $c[$d]=$f $d=$d+1 Endif $f=$g-1 Else If $g-1 > $e $b[$d]=$e $c[$d]=$g-1 $d=$d+1 Endif $e=$g+1 Endif Loop $d=$d-1 Loop $qs=$a Endfunction
Edited by Howard Bullock (2006-06-17 02:13 AM)
|
Top
|
|
|
|
#163387 - 2006-06-17 02:48 AM
Re: Find In Array
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11625
Loc: CA
|
Quote:
Dont need exact code, but some pointers.
Now look what you've gone and done Howard. LOL
|
Top
|
|
|
|
#163388 - 2006-06-17 03:44 AM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
That seemed like the easiest pointer I could provide...
|
Top
|
|
|
|
#163392 - 2006-06-17 03:31 PM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Yes... If he needs the element numbers of the original array, the sorting will be a problem. Also, if that is the case, he would need to specify how to handle the duplicates values. Should all of them be reported etc.
|
Top
|
|
|
|
#163393 - 2006-06-17 03:33 PM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
What analysis are you doing? What is the real issue that needs to be solved?
|
Top
|
|
|
|
#163395 - 2006-06-17 04:24 PM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
this seems to work...
Code:
$previousstate = SetOption( "NoVarsInStrings", "ON" )
dim $A[9] $A[0] = 1 $A[1] = 7 $A[2] = 3 $A[3] = 4 $A[4] = 5 $A[5] = 5 $A[6] = 7 $A[7] = 2 $A[8] = 1 $A[9] = 1
$sorted = qs($A)
for each $x in $sorted ? $x next ? $UniqueArray = GetFirstFiveUnique($A) for each $x in $UniqueArray ? $x next
$answer = FindIndexes($A, $UniqueArray) for $x=0 to ubound($answer,1) ? "Value of " + $answer[$x,0] ? "Is at these indexes: " for each $index in $answer[$x,1] " " + $index next ? next
function GetFirstFiveUnique($Array) dim $i, $x, $y, $sorted, $Result[0]
$sorted = qs($Array) $x = 0 for $i=0 to ubound($sorted) if $i=0 $Result[0] = $sorted[0] $x = 1 + $x endif if $i > 0 if $Result[$x-1] <> $sorted[$i] Redim Preserve $Result[$x] $Result[$x] = $sorted[$i] $x = 1 + $x endif endif if $x = 5 $i = 1 + ubound($Array) endif next $GetFirstFiveUnique = $Result
endfunction
function FindIndexes ($OrigArray, $ArrayOfFive) dim $results[4,1], $x, $y, $z
for $z=0 to ubound($ArrayOfFive)
dim $ElementIndex[0] $y=0 for $x=0 to ubound($OrigArray) if $ArrayOfFive[$z] = $OrigArray[$x] Redim Preserve $ElementIndex[$y] $ElementIndex[$y] = $x $y = 1 + $y endif next $results[$z, 0] = $ArrayOfFive[$z] $results[$z, 1] = $ElementIndex $FindIndexes = $results next endfunction
function qs($a) DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l $b[0]=0 $c[0]=UBOUND($a) $d=0 While $d >=0 $e=$b[$d] $f=$c[$d] While $e < $f $h=$e+($f-$e)/2 $k=$a[$e] $A[$e]=$A[$h] $A[$h]=$k $i=$e+1 $j=$f $l=0 Do While ($i<$j) AND $A[$e] > $A[$i] $i=$i+1 Loop While ($j>=$i) AND $A[$j] > $A[$e] $j=$j-1 Loop IF $i>=$j $l=1 ELSE $k=$A[$i] $A[$i]=$A[$j] $A[$j]=$k $j=$j-1 $i=$i+1 ENDIF Until $l=1 $k=$a[$e] $a[$e]=$a[$j] $a[$j]=$k $g=$j If $g-$e <= $f - $g If $g+1 < $f $b[$d]=$g+1 $c[$d]=$f $d=$d+1 Endif $f=$g-1 Else If $g-1 > $e $b[$d]=$e $c[$d]=$g-1 $d=$d+1 Endif $e=$g+1 Endif Loop $d=$d-1 Loop $qs=$a Endfunction
|
Top
|
|
|
|
#163401 - 2006-06-20 04:06 AM
Re: Find In Array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Have you executed my latest code?
Code:
c:\data\scripts>..\kix\kix.450\kix32 test.kix
1 1 1 2 3 4 5 5 7 7 1 2 3 4 5 Value of 1 Is at these indexes: 0 8 9 Value of 2 Is at these indexes: 7 Value of 3 Is at these indexes: 2 Value of 4 Is at these indexes: 3 Value of 5 Is at these indexes: 4 5 c:\data\scripts>
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 323 anonymous users online.
|
|
|