PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Hashmap/array-like thing in sh
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Hashmap/array-like thing in sh

Réponse
 
LinkBack Outils de la discussion
Vieux 24/07/2007, 16h34   #1
John
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Hashmap/array-like thing in sh

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.

  Réponse avec citation
Vieux 24/07/2007, 16h40   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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.
>

  Réponse avec citation
Vieux 24/07/2007, 16h43   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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.
  Réponse avec citation
Vieux 24/07/2007, 16h43   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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.
  Réponse avec citation
Vieux 24/07/2007, 18h25   #5
John
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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

  Réponse avec citation
Vieux 24/07/2007, 23h38   #6
bsh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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

  Réponse avec citation
Vieux 25/07/2007, 10h22   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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
  Réponse avec citation
Vieux 11/08/2007, 03h07   #8
William Park
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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/
  Réponse avec citation
Vieux 11/08/2007, 13h54   #9
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Hashmap/array-like thing in sh

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14759 seconds with 17 queries