【已解决】如何判断删除为空内容的数组?【感谢各位活动分子热心帮助】
本帖最后由 huangke 于 2013-1-22 14:11 编辑#include <Array.au3>
Local $avArray
$avArray = "JPM"
$avArray = "Holger"
$avArray = " "
$avArray = "Larry"
$avArray = " "
$avArray = "Valik"
$avArray = " "
$avArray = "Nutster"
$avArray = " "
$avArray = " "
_ArrayDisplay($avArray, "$avArray 原 _ArrayDelete()")
_ArrayDelete($avArray, 2)
_ArrayDelete($avArray, 4)
_ArrayDelete($avArray, 6)
_ArrayDelete($avArray, 8)
_ArrayDelete($avArray, 9)
_ArrayDisplay($avArray, "$avArray 后 _ArrayDelete()")
这样删除的话会出错的,因为数组个数变动了,空内容的ID会变,如:删除了$avArray之后,$avArray就会变成$avArray了,待到_ArrayDelete($avArray, 8)的时候,$avArray已经不是开始时候的$avArray了。。。{:face (382):}
有哪位大侠知道如何判断呢?
发帖两小时就解决,说明很简单嘛!给完整DD。
感谢 kevinch,afan,水木子(正则偷他的,嘻嘻!{:face (355):})
#include <Array.au3>
Local $avArray
Dim $dic=ObjCreate("scripting.dictionary")
$avArray = "JPM"
$avArray = "Holger"
$avArray = " "
$avArray = "Larry"
$avArray = " "
$avArray = "Valik"
$avArray = " "
$avArray = "Nutster"
$avArray = " "
$avArray = " "
_ArrayDisplay($avArray, "$avArray 原 _ArrayDelete()")
$i=1
For $n=0 To UBound($avArray)-1
$str = StringRegExpReplace($avArray[$n], "(?m)^\s+|(?m)\s+$", "")
If $str<>"" Then
$dic($i)=$avArray[$n]
$i+=1
EndIf
Next
$avArray=$dic.items
$dic=Null
_ArrayDisplay($avArray, "$avArray 后 _ArrayDelete()")
Ubound函数.. 可以排序的话先排序查找第一个空元素重定义数组,不许排序的话交换数组元素,重定义数组
如果数组不大的话,输出成字符串,替换所有的‘||’,重新拆 回复 1# huangke
把不为空的数组内容赋值给新的数组用_ArrayAdd()函数 #include <Array.au3>
Local $avArray
Dim $dic=ObjCreate("scripting.dictionary")
$avArray = "JPM"
$avArray = "Holger"
$avArray = " "
$avArray = "Larry"
$avArray = " "
$avArray = "Valik"
$avArray = " "
$avArray = "Nutster"
$avArray = " "
$avArray = " "
_ArrayDisplay($avArray, "$avArray 原 _ArrayDelete()")
$i=1
For $n=0 To UBound($avArray)-1
If $avArray[$n]<>" " Then
$dic($i)=$avArray[$n]
$i+=1
EndIf
Next
$avArray=$dic.items
$dic=Null
_ArrayDisplay($avArray, "$avArray 后 _ArrayDelete()")这个试下 示例所谓的空其实也非空,含有空格。
如果如示例所述,需要删除的元素比较多,还是输出成字符串再用正则比较好。 这个试下
kevinch 发表于 2013-1-22 12:54 http://www.autoitx.com/images/common/back.gif
嗯,这个也是一个办法,将不为空的数值赋值给一个新的数组,谢谢啊! 示例所谓的空其实也非空,含有空格。
如果如示例所述,需要删除的元素比较多,还是输出成字符串再用正则比 ...
afan 发表于 2013-1-22 12:56 http://www.autoitx.com/images/common/back.gif
对哦,先正则掉空的东西,再用 kevinch 那个,不错不错! 对哦,先正则掉空的东西,再用 kevinch 那个,不错不错!
huangke 发表于 2013-1-22 13:51 http://www.autoitx.com/images/common/back.gif
不用先去掉,最后一次性取即可#include <Array.au3>
Local $avArray
$avArray = "JPM"
$avArray = "Holger"
$avArray = " "
$avArray = "Larry"
$avArray = " "
$avArray = "Valik"
$avArray = " "
$avArray = "Nutster"
$avArray = " "
$avArray = " "
Local $str = '|'
For $n = 0 To UBound($avArray) - 1
$str &= $avArray[$n] & '|'
Next
$avArray = StringRegExp($str, '[^\|]*?[^\|\s][^\|]*', 3)
_ArrayDisplay($avArray) 不用先去掉,最后一次性取即可
afan 发表于 2013-1-22 14:07 http://www.autoitx.com/images/common/back.gif
{:face (280):}噢耶,原来这样啊。。。。算法始终高新手九条街啊 回复 9# afan
Afan的正则真是令人崇拜~我写了个函数,可以删除指定的字符元素。
#include <Array.au3>
Dim $avArray, $newavArray
$avArray = "JPM"
$avArray = "Holger"
$avArray = " "
$avArray = "Larry"
$avArray = " "
$avArray = "Valik"
$avArray = " "
$avArray = "Nutster"
$avArray = " "
$avArray = " "
_ArrayDisplay($avArray, "$avArray 原 _ArrayDelete()")
$newavArray = _ArrayDel_Element($avArray, " ")
_ArrayDisplay($newavArray, "$avArray 后 _ArrayDelete()")
Func _ArrayDel_Element($hfile, $element)
Local $flag = 0, $NUM = 0
Local $arr = $hfile
For $i = 0 To UBound($hfile) - 1
If $arr[$i] = $element Then
$flag = $flag + 1
Else
$hfile[$NUM] = $arr[$i]
$NUM = $NUM + 1
EndIf
Next
For $i = (UBound($hfile) - 1) To (UBound($hfile) - $flag) Step -1
_ArrayDelete($hfile, $i)
Next
Return $hfile
EndFunc ;==>_ArrayDel_Element
页:
[1]