* let unset array element remove compatible with bash
@ 2012-02-22 3:28 Daniel Lin
2012-02-22 5:41 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Lin @ 2012-02-22 3:28 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 983 bytes --]
As we know, bash is very common on Linux world.
So, even I've moved to zsh.
But, I wish my script could be compatible with bash.
Today, I found the array element remove is not compatible with bash.
Can any developer consider to enhance zsh's function like "unset var[2]"?
Here is the sample
$var[1]="four" ; var[2]="seven" ; var[3]="Four and
Seven"
$for v in "${var[@]}" ; do echo "$v" ;
done
four
seven
Four and
Seven
$for v in "${var[*]}" ; do echo "$v" ;
done
four seven Four and
Seven
$echo "${#var[@]} ${#var[1]} ${#var[2]} ${#var[3]}" # get
lengh
3 4 5
14
$unset var[2] ###### BASH only delete one
element
$var[2]=() ###### ZSH only delete one
element
$echo "${#var[@]} ${#var[1]} ${#var[2]}
${#var[3]}"
2 4 0 14
Another note, I wish the FAQ in zsh can explain this incompatible with bash.
$echo "${#var[@]} ${#var[1]} ${#var[2]} ${#var[3]}" # get lengh work both
bash/zsh
$echo "$#var $#var[1] $#var[2] $#var[3]" # get lengh work only zsh
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: let unset array element remove compatible with bash
2012-02-22 3:28 let unset array element remove compatible with bash Daniel Lin
@ 2012-02-22 5:41 ` Bart Schaefer
2012-02-22 14:18 ` Chet Ramey
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2012-02-22 5:41 UTC (permalink / raw)
To: zsh-users
On Feb 22, 3:28am, Daniel Lin wrote:
}
} Can any developer consider to enhance zsh's function like "unset var[2]"?
}
} $unset var[2] ###### BASH only delete one element
} $var[2]=() ###### ZSH only delete one element
I've started a thread on zsh-workers about this, but:
var[3]=() does not mean the same thing that unset var[3] would imply.
In zsh, if you assign to a position that is "off the end" of the array,
zsh manufactures empty array elements to "fill in the gap". Try this
in each of bash and zsh:
unset gappy
gappy[9]=nine
for g in "${gappy[@]}"; do echo /$g/; done
Now in zsh:
unset gappy
gappy=(one)
gappy[3]=()
for g in "${gappy[@]}"; do echo /$g/; done
Note that assigning an empty array to the one-element slice gappy[3]
has caused gappy[2] to exist as an empty element.
Bash is using some kind of sparse structure to store its arrays.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: let unset array element remove compatible with bash
2012-02-22 5:41 ` Bart Schaefer
@ 2012-02-22 14:18 ` Chet Ramey
0 siblings, 0 replies; 3+ messages in thread
From: Chet Ramey @ 2012-02-22 14:18 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-users, chet.ramey
On 2/22/12 12:41 AM, Bart Schaefer wrote:
> Bash is using some kind of sparse structure to store its arrays.
FWIW, bash uses a doubly-linked list.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-02-22 14:24 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-22 3:28 let unset array element remove compatible with bash Daniel Lin
2012-02-22 5:41 ` Bart Schaefer
2012-02-22 14:18 ` Chet Ramey
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).