help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Associative array questions


From: John Kearney
Subject: Re: [Help-bash] Associative array questions
Date: Sat, 07 Jul 2012 22:18:08 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1

Am 07.07.2012 21:54, schrieb Bill Gradwohl:
#!/usr/bin/bash

func(){
IFS=$'\n';
vars=([black]=night [white]=snow
$(cat /tmp/xxx) [yellow]=submarine
[pink]=elephant)
IFS=$' \t\n'
declare -p vars
}

declare -A vars

echo '[red]=rose'     >/tmp/xxx
echo '[green]=grass' >>/tmp/xxx
echo '[blue]=sky'    >>/tmp/xxx

func
echo
echo '[red]=rose'     >/tmp/xxx
echo '[green]=grass' >>/tmp/xxx
echo -n '[blue]=sky'    >>/tmp/xxx

func

produces:

/tmp/yyy: line 7: vars: [red]=rose: must use subscript when assigning associative array
/tmp/yyy: line 7: vars: [green]=grass: must use subscript when assigning associative array
/tmp/yyy: line 7: vars: [blue]=sky: must use subscript when assigning associative array
declare -A vars='([yellow]="submarine" [white]="snow" [black]="night" [pink]="elephant" )'

/tmp/yyy: line 7: vars: [red]=rose: must use subscript when assigning associative array
/tmp/yyy: line 7: vars: [green]=grass: must use subscript when assigning associative array
/tmp/yyy: line 7: vars: [blue]=sky: must use subscript when assigning associative array
declare -A vars='([yellow]="submarine" [white]="snow" [black]="night" [pink]="elephant" )'

1) Why does the cat not work? What is it REALLY complaining about, as the individual lines are formatted properly.

what you are thinking about is more like this
func(){
   IFS=$'\n';
  eval vars=([black]=night [white]=snow
$(cat /tmp/xxx) [yellow]=submarine
[pink]=elephant)
IFS=$' \t\n'
declare -p vars
}
but don't do that just do someting like this
func(){
  vars=([black]=night [white]=snow [yellow]=submarine [pink]=elephant)
  while read -r i ; do
    vars[
${i%%=*}]="${i#*=}"
  done
  declare -p vars
}
echo 'red=rose'     >/tmp/xxx
echo 'green=grass' >>/tmp/xxx
echo 'blue=sky'    >>/tmp/xxx


2) Since IFS is set to just the newline character, I was surprised to see that [black] does not equal 'night [white]=snow' . It is still breaking things up on the space.
has to be set before parsing of the code to take effect.
IFS=$'\n';
func(){
  eval vars=([black]=night [white]=snow
$(cat /tmp/xxx) [yellow]=submarine
[pink]=elephant)
declare -p vars
}
IFS=$' \t\n'

the way you have set IFS it only effects expanded variables.

basically its not really practical to use IFS in the way you are thinking.

3) In the second execution of func, the last line of the file does not contain a newline and yet its able to locate the [yellow]= and isolate it from whatever it doesn't like on the last line of the cat. The IFS appears to be ignored in locating the keys. Are keys located first and then whatever is between keys is the value?
no not quiet. See above


--
Bill Gradwohl



--
View John
          Kearney's profile on LinkedIn John Kearney

reply via email to

[Prev in Thread] Current Thread [Next in Thread]