#124191 - 2004-07-30 07:37 PM
Remove element from array
|
Jose
Seasoned Scripter
   
Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
|
Hi Korges uts up? I am trying to remove the blank elements from an array, I have tryed with DelHashKey() with no success, this is the code.
Code:
Dim $y, $Array $Array = "A1", "B2", "C3", "", "C5", "", "C7"
For Each $r In $Array $y=$y+1 If $r="" DelHashKey("$r","$y") EndIf Next
As this REMARK in DelHashKey was given by Howard: Quote:
This function deletes the key/value pair from the hash named $HashName.
I thought that the value=$y and key=$r but I think I am wrong. Any idea how could I remove the "" elements? Thanks a lot.
_________________________
Life is fine.
|
|
Top
|
|
|
|
#124192 - 2004-07-30 07:45 PM
Re: Remove element from array
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Jose basta-buddy, ... Here's a quick concept script ... uses a quick udf called TrimArray which may-or-may-not be in some udflib somewhere, idk ...
Code:
Dim $y, $Array
$Array = "A1", "B2", "C3", "", "C5", "", "C7"
$NewArray = TrimArray($Array)
For Each $Element In $NewArray
?"Element=" $Element
Next
Exit 0
Function TrimArray($Array)
dim $i, $element, $ar[UBound($Array)]
$i = 0
For Each $Element In $Array
If $Element
$ar[$i] = $Element
$i = $i + 1
Endif
Next
If $i
REDIM PRESERVE $ar[$i-1]
Endif
$TrimArray = $ar
EndFunction
-Shawn
|
|
Top
|
|
|
|
#124195 - 2004-07-30 08:36 PM
Re: Remove element from array
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
How about checking the LEN of the var?
Code:
Dim $y, $Array
$Array = "A1", "B2", "C3", "", "C5", "", "C7", 0
$NewArray = TrimArray($Array)
For Each $Element In $NewArray ?"Element=" $Element Next
Exit 0
Function TrimArray($Array)
dim $i, $element, $ar[UBound($Array)]
$i = 0
For Each $Element In $Array
If Len($Element)
$ar[$i] = $Element $i = $i + 1
Endif
Next
If $i REDIM PRESERVE $ar[$i-1] Endif
$TrimArray = $ar
EndFunction
|
|
Top
|
|
|
|
#124196 - 2004-07-30 08:38 PM
Re: Remove element from array
|
Jose
Seasoned Scripter
   
Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
|
Dont take this serious pls. I just gave the $ItemToRemove to remove as parameter. je je je. Code:
;FUNCTION: TrimArray() ; ;ACTION: Removes given element of a given array. ; ;AUTHOR: DON QUIJOTE DE LA MANCHA (Shawn Tassie) ; ;CONTRIBUTORS: Some south basta ; ;DATE CREATED: 2004/07/30 ; ;KIXTART: any ; ;SYNTAX: TrimArray($Array,$ItemToRemove) ; ;PARAMETERS: $Array ; Array from where you want to remove items ; ; $ItemToRemove ; Single Item to remove from array ; ;RETURNS: New array without $ItemToRemove ; ;DEPENDENCIES: none ; ;EXAMPLE: Dim $y, $Array ; $Array = "A1", "B2", "C3", "C4", "D5", "A1", "C1" ; $NewArray = TrimArray($Array,"A1")
Function TrimArray($Array,$ItemToRemove) Dim $i, $ar[Ubound($Array)] $i = 0 For Each $Element In $Array If $Element<>$ItemToRemove $ar[$i] = $Element $i = $i + 1 EndIf Next If $i ReDim PRESERVE $ar[$i-1] EndIf $TrimArray = $ar EndFunction
_________________________
Life is fine.
|
|
Top
|
|
|
|
#124197 - 2004-07-30 08:40 PM
Re: Remove element from array
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
you can use this....
Code:
$Array = "A1", "B2", "C3", "", "C5", "", "C7" ? ubound($array)
$d = "|" $array = split(join(split(join($array,$d),$d+$d),$d),$d) ? ubound($array)
just make sure that the delim $d does not equal valid array data 
Bryce
|
|
Top
|
|
|
|
#124198 - 2004-07-30 08:50 PM
Re: Remove element from array
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Beautiful! How about:
Code:
Function TrimArray($Array)
dim $d
$d = CHR(10)
$TrimArray = split(join(split(join($array,$d),$d+$d),$d),$d)
EndFunction
[edit] but Jose's removes a user-specified element ! Nice.
|
|
Top
|
|
|
|
#124199 - 2004-07-30 09:15 PM
Re: Remove element from array
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
The DelHashKey("$r","$y") UDF is part of the HASH udf collection. If you check the code you will see that the Array name submitted is altered in the UDF. I did this to avoid potential conflicts with other arrays used in the program.
$HashKey = $HashName + "key" $HashValue = $HashName + "value"
The concept is valid for what you tried Jose, buit the code is all structure to work with arrays created by the UDF Function Hash($HashName, $Key, optional $Value)
Did you try the entire set of UDFs? You may find the use of HASH very easy. and it provides a way to remove those undesired values.
Link: Hash() Library UDF's
|
|
Top
|
|
|
|
#124200 - 2004-07-30 10:41 PM
Re: Remove element from array
|
Jose
Seasoned Scripter
   
Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
|
Thanks for the UDF tips Howard. Now I see the utility they have thought I coudnt help play with Shawn sample and gave more functionality.
With this sample $Remove can be a an array or a string and will be removed too.
Code:
Dim $y, $Array, $ArrayToRemove
$Array = "A1", "B2", "C3", "C4", "D5", "A1", "C1"
$Remove="A1","C1"
$NewBastaArray = TrimArray($Array,$Remove)
For Each $basta In $NewBastaArray
? $basta
Next
Function TrimArray($Array,$Remove)
Dim $i, $ar[Ubound($Array)]
$i = 0
Select
Case VarType($Remove)=8
For Each $Element In $Array
If $Element<>$Remove
$ar[$i] = $Element
$i = $i + 1
EndIf
Next
Case VarType($Remove)=8204
For Each $Element In $Array
$ExistRemove=AScan($Remove, $Element)
If $ExistRemove = -1
$ar[$i] = $Element
$i = $i + 1
EndIf
Next
Case VarType($Remove)<>8204 OR VarType($Remove)<>8
? "Not supperted value"
EndSelect
If $i
ReDim PRESERVE $ar[$i-1]
EndIf
$TrimArray = $ar
EndFunction
Intresting isnt it?
_________________________
Life is fine.
|
|
Top
|
|
|
|
#124203 - 2004-07-30 11:34 PM
Re: Remove element from array
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
BTW, shouldn't you be doing bitwise on the following...
Code:
... Case VarType($Remove) & 8 ... Case VarType($Remove) & 8192 ...
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|