shell - bash 4.x assoc array loses keys in receiving function -


I am trying to pass an associative array from one function to another, and losing the nominated index keys ( E.g., file path, search in the example below) Although the correct use of the elements in the array can be indexed using 0, 1. I have to do something wrong with Bash syntax, but it is not known at all about any help appreciated.

Using GNU bash, version 4.3.8 from Ubuntu 14.04 is just a bash code example below, and the output is below

  #! / Bin / bash function test_function {func_data = ("$ {@}") # above func_data without brackets [1] # local $ {! Func_data} # can not access the above mentioned local statement in EcoK "$ {func_data [@]}" by any means; Do $ K; Echo "done" echo "func_data: $ {func_data}" echo "func_data [filepath]: $ {func_data [filepath]}" echo "func_data [search]: $ {func_data [search]}" # three production up Echo first array The element of which is the "style" during the first loop, {0} array elements can reach 0, 1, but no longer through the file path, search resonant "func_data [0]: $ {func_data [0]}" Echo "func_data [1]: $ {func_data [1]}" echo "! Func_data [@]: $ {! Func_data [@]} "output above # echo" 0 1 "so indexes are zero based? Echo" func_data [@]: $ {func_data [@}} "# Akor up style of the array elements" default "{~ / .gtkrc-2.0 '} # Output # in BASH, the local variable scope is called from the current function and every # hair function, so provide a function for # so that the variable to fix the issues It is possible to use the realm. Main {echo declare-A gtkrc2 = () gtkrc2 [filepath] = "~ / .gtkrc-2.0" gtkrc2 [search ] = "Style \" default \ "{" echo "gtkrc2 filepath: $ {gtkrc2 [filepath]}" echo "gtkrc2 looking for: $ {gtkrc2 [search]}" test_function "$ {gtkrc2 [@]}" echo "" Declared - a gtkcss = () gtkcss [filepath] = "~ / .config / gtk-3.0 / gtk.css" gtkcss [search] = "scrollbar {echo" gtkcss filepath :. $ {Gtkcss [filepath]} "Echo" gtkcss search: $ {gtkcss [search]} "test_function" $ {gtkcss [@]} "} main   

------ ---- Output - --------

  gtkrc2 file path: ~ / .gtkrc-2.0 gtkrc2 search: style "default" {func_data: style "default" {func_data [ Filepath] style "default" {Frnnka_deta [search] style "default" {Frnnk_deta [0]: style "default" {func_data [1]: ~ / .gtkrc-2.0! Func_data [@]: 0 1 Fnc_deta [@ ] style "default" {~ / .gtkrc-2.0 gtkcss Failpth: ~ / .config /gtk-3.0/gtk.css gtkcss search: .scrollbar {func_data: .scrollbar {func_data [filepath]: .scrollbar {func_data [search ]: .scrollbar {func_data [0]: .scrollbar {func_data [1]: ~ / .config / gtk-3.0 / gtk.css! Func_data [@]: 0 1 FnD_Data [@]: .scrollbar {~ / .config / gtk-3.0 / gtk.css    

This may not be the "right" way to do this, but it is best I can understand. Any suggestions from others are welcome: $ idxlist for idx

  Function test_function {arrname = $ 1 idxlist = "$ 2" resonates resonant "array pass = $ arrname"; In Busy, the local variable scope is the current function and every # child function is called, so provide the function, for example, the name of the name is Ams = $ arrname [$ idx] echo "idx = $ idx, elem = $ {elemname } "Done} to main # makes it possible to use the variable to fix these problems. Main {echo declare-A gtkrc2 = () gtkrc2 [filepath] =" ~ / .gtkrc-2.0 "gtkrc2 [search ] = "style \" default \ "{" echo "gtkrc2 filepath: $ {gtkrc2 [filepath]}" echo "gtkrc2 looking for: $ {gtkrc2 [search]}" test_function gtkrc2 "! $ {gtkrc2 [*]}" echo Declared "gtkcs" S = () gtkcss [filepath] = "~ / .config / gtk-3.0 / gtk.css" gtkcss [search] = echo "gtkcss filepath: $ {gtkcss [filepath]}" "scrollbar {." Echo "gtkcss search: $ {Gtkcss [search]}" test_function gtkcss "$ {! Gtkcss [*]}"} Main   

Specifically:

    < Li> The function to pass each associative array, we pass both the array names and the list of directors
  • inside the function, the list of array names and indices are taken from the statusal parameters
  • We might get loop above the list of indexes and get the respective values ​​of each element This is done by generating the name of the element for the first time, and then to get the actual value! .

    Describes this technique of arrays, but only addresses indexed arrays, and not associative array; Passing the indicator list is one of the ways I can think it works for such a helper.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -