|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
Is there any hashmap or array-like construct in sh? Something that allows you to index into a structure. Preferably something like a hashmap, so you can do things like echo $msg[$index] instead of if [ $index = "hello" ] echo blabla elif [ $index = "howdy" ] echo thisandthat .... But if it's only possible to index with numbers, that is fine too. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
John wrote:
> Hi > > Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like Not in POSIX sh. If you happen to use a ksh93 there are associative arrays. Janis > > echo $msg[$index] > > instead of > > if [ $index = "hello" ] > echo blabla > elif [ $index = "howdy" ] > echo thisandthat > ... > > But if it's only possible to index with numbers, that is fine too. > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
John wrote:
> Hi > > Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like > > echo $msg[$index] > > instead of > > if [ $index = "hello" ] > echo blabla > elif [ $index = "howdy" ] > echo thisandthat > ... > > But if it's only possible to index with numbers, that is fine too. > Assuming that by "sh" you mean "shell" in general rather than specifically old Bourne sh: yes, there's arrays and yes you can only index them by numbers. Try this program to see how it behaves in your shell: array=( zero one two three four ) echo "${array[@]}" echo "${array[*]}" echo "${array[0]}" echo "${array[1]}" echo "${#array[@]}" for var in ${array[@]} do echo "$var" done and see http://tldp.org/LDP/abs/html/arrays.html for more information BUT if you're using shell arrays, chances are your approach is wrong and you should be using awk or perl or equivalent instead (and yes, they do have associative arrays). Ed. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
John wrote:
> Hi > > Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like > > echo $msg[$index] > > instead of > > if [ $index = "hello" ] > echo blabla > elif [ $index = "howdy" ] > echo thisandthat > ... > > But if it's only possible to index with numbers, that is fine too. > Assuming that by "sh" you mean "shell" in general rather than specifically old Bourne sh: yes, there's arrays and yes you can only index them by numbers. Try this program to see how it behaves in your shell: array=( zero one two three four ) echo "${array[@]}" echo "${array[*]}" echo "${array[0]}" echo "${array[1]}" echo "${#array[@]}" for var in ${array[@]} do echo "$var" done and see http://tldp.org/LDP/abs/html/arrays.html for more information BUT if you're using shell arrays, chances are your approach is wrong and you should be using awk or perl or equivalent instead (and yes, they do have associative arrays). Ed. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jul 24, 5:43 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> Assuming that by "sh" you mean "shell" in general rather than > specifically old Bourne sh: yes, there's arrays and yes you can only > index them by numbers. I did mean that old crappy thing. > Try this program to see how it behaves in your shell: > array=( zero one two three four ) > echo "${array[@]}" > echo "${array[*]}" > echo "${array[0]}" > echo "${array[1]}" > echo "${#array[@]}" > > for var in ${array[@]} > do > echo "$var" > done I get "Syntax error: "(" unexpected". > and seehttp://tldp.org/LDP/abs/html/arrays.htmlfor more information > BUT if you're using shell arrays, chances are your approach is wrong and > you should be using awk or perl or equivalent instead (and yes, they do > have associative arrays). So I guess awk will be my friend (after learning it). Thanks for all the fish -- John |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
John <jan...@gmail.com> wrote:
> Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like > $ echo $msg[$index] > But if it's only possible to index with numbers, that is fine too. As previously bemoaned, you do not indicate whether you have the bourne shell, an old bash shell, or kornshell88 to work with. All of these shells do not implement variable hash indices, AKA hashes. However, in general, everything is possible... somehow. Also -- in general -- it just may not be worth the effort to kludge in a facility which is not natively implemented. If you have a shell that implements integer-indexed variables, AKA arrays, then you might try the following library written in ksh and which may be ported to bash: ftp://ftp.armory.com/pub/lib/ksh/AssocArr/ Heck, I once wrote a shell-in-a-shell in bourneshell script that implements aliases, command-line editing, tilde-substitution, and history.... So, good luck! =Brian |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
2007-07-24, 15:34(-00), John:
> Hi > > Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like > > echo $msg[$index] [...] No, but remember a shell is a command interpreter, and some commands are interpreters to programming language. If you need associative arrays, chances are that you want to program something not to do it shell way. In any case, in shell, you can also do val=xxx eval "val$index=\$val" and eval "val=\$val$index" awk, perl, python, tcl interpreters are known to have associative array support. -- Stéphane |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
John <janzon@gmail.com> wrote:
> Hi > > Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like > > echo $msg[$index] > > instead of > > if [ $index = "hello" ] > echo blabla > elif [ $index = "howdy" ] > echo thisandthat > ... > > But if it's only possible to index with numbers, that is fine too. If it's strictly [0-9A-Z] or [0-9a-zA-Z], then you can convert it to base36 or base62 number, and use shell array. Otherwise, use GDBM. -- William Park <opengeometry@yahoo.ca>, Toronto, Canada BashDiff: Super Bash shell http://freshmeat.net/projects/bashdiff/ |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
2007-07-24, 15:34(-00), John:
> Is there any hashmap or array-like construct in sh? Something that > allows you to index into a structure. Preferably something like a > hashmap, so you can do things like [...] Yes awk has hashes and is a standard Unix utility you can call in a sh script. Remember a shell is a command to execute commands. Some shells like zsh and some versions of ksh93 have built in support for associative arrays. Only zsh support binary values for either key or value though. Using those should be your last resort though. -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|