help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Associative array questions


From: Bill Gradwohl
Subject: [Help-bash] Associative array questions
Date: Sat, 7 Jul 2012 13:54:32 -0600

#!/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.

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.

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?

--
Bill Gradwohl


reply via email to

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