https://www.cnblogs.com/yeungchie/
函式功能
Perl 中 shift 函式可以回傳串列的第一個元素,并將后續所有元素向前移位( 索引值減 1 ),輸入可選,默認為 @_ 或者 @ARGV,
my @foo = qw( 1 2 3 4 );
say shift @foo;
# 1
say @foo;
# 234
在某些版本的 Tcl 中函式 lshift 可以實作類似效果,或者使用 struct::list 包:
package require struct::list
set foo { 1 2 3 4 }
puts [::struct::list shift foo]
# 1
puts $foo
# 2 3 4
但某些 EDA 工具中內置的 Tcl 版本較舊,或者做了精簡,無法使用上面提到的方法,所以下面自己來實作一下,
Tcl 實作
proc shift { sym } {
upvar 1 $sym foo
if { ! [info exists foo] } {
error "can't read \"$sym\": no such variable"
}
set foo [lassign $foo[unset foo] var0]
return $var0
}
測驗
串列迭代
set a {3 1 2 5 4}
# 3 1 2 5 4
shift a
# 3
shift a
# 1
puts $a
# 2 5 4
回圈中應用
set a {1 2 {} 4 {}}
# 1 2 {} 4 {}
while { [llength $a] } {
puts [shift a]
}
# 1
# 2
#
# 4
#
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543065.html
標籤:其他
下一篇:用GPU來運行Python代碼
