Page 1 of 1 1
Topic Options
#124191 - 2004-07-30 07:37 PM Remove element from array
Jose Offline
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 Offline
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
#124193 - 2004-07-30 08:04 PM Re: Remove element from array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Thanks Shawn I had the feeling you where gonna answer, as allways there was a smile in my face when you did.
Works perfect.

No, havent seen this UDF nor at Korg neither at Lonkero´s site and it is kinda important dont you think?.
Let me try and give some format.

_________________________
Life is fine.

Top
#124194 - 2004-07-30 08:11 PM Re: Remove element from array
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Might want to change that conditional in there, because right now it will remove anything that is "false" - that would include "" and 0 and nothing etc, might need to give that some tought, maybe change to check for just "" ? idk
Top
#124195 - 2004-07-30 08:36 PM Re: Remove element from array
Chris S. Offline
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 Offline
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 Offline
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 Offline
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 Offline
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
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#124200 - 2004-07-30 10:41 PM Re: Remove element from array
Jose Offline
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
#124201 - 2004-07-30 10:49 PM Re: Remove element from array
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Pardon me for jumping in this late in the game but is there any reason why one of the two UDFs ArrayPack() and RemoveDups() would not work for you?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#124202 - 2004-07-30 10:57 PM Re: Remove element from array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
lol Les you are right.
Still I liked this way cause made me undertand a little more hou UDF´s work.
Thanks indeed.
_________________________
Life is fine.

Top
#124203 - 2004-07-30 11:34 PM Re: Remove element from array
Chris S. Offline
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
#124204 - 2004-07-30 11:41 PM Re: Remove element from array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
jose, not sure what you are upto.
simple search on my listing with "remove element array" gives you:
http://www.gwspikval.com/jooel/UDF/udf/83217.htm
_________________________
!

download KiXnet

Top
#124205 - 2004-07-31 12:55 AM Re: Remove element from array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Thanks for the data my friend Lonkero, I will try a little harder on the search before next time.

Chris:
This worked for me.

When using $Remove="A1" I get Vartype=8 (String)
When using $Remove="A1","D5" I get Vartype=8204 (Array of variants)
Took it from Vartype sample in the manual.

Was this what you where pointing me ?


_________________________
Life is fine.

Top
#124206 - 2004-07-31 01:02 AM Re: Remove element from array
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
udf's are convenient to be sure, but thats about it. there's something to be said about understanding wtf your using.
Top
#124207 - 2004-08-01 09:38 AM Re: Remove element from array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
I found RemFromArr() does exactly the same as worked out in the sample. Amazing.

Thanks all....
_________________________
Life is fine.

Top
#124208 - 2004-08-01 03:50 PM Re: Remove element from array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what?
you found?
just some moments ago you wanted to learn yourself and know you are going after some udf?
and my suggestion was not good enough for you even though it does what you asked?

oh boy...
_________________________
!

download KiXnet

Top
#124209 - 2004-08-01 07:38 PM Re: Remove element from array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Sorry Lonkero .
All suggestions fitted I know. Be patient on me pls.
Me not one of those UDF maker member, me still running uphill to make them work.lol.

Yesterday while browsing all the UDF´s by Member I was thinking......"My god these guys had made a good job here".

Thanks.
_________________________
Life is fine.

Top
#124210 - 2004-08-01 08:30 PM Re: Remove element from array
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ey, me not mad.
just wonder your comment about finding something.
when I just had pointed you a perfect fit.
well, does not matter no more.
_________________________
!

download KiXnet

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 (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.132 seconds in which 0.108 seconds were spent on a total of 12 queries. Zlib compression enabled.

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