Sponsored Content
Top Forums Shell Programming and Scripting Bash Array connectin to another Array Post 303003408 by rbatte1 on Wednesday 13th of September 2017 12:22:35 PM
Old 09-13-2017
Hello batchenr,

I think you probably want to use an Associative Array, i.e an unordered list of key to value pairs. Have a look in the man bash pages for the basic information and skip forward to the Arrays section, about 90% of the way down on my display.

Associative arrays are created using declare -A array_name and you add and use values like this:-
Code:
#!/bin/bash

declare -A array_name

array_name["Key1"]="Value1"
array_name["Key2"]="Value2"
array_name["Key3"]="Value3"
array_name["Key4"]="Value4"

for testkey in Key3 Key1
do
   echo "${array_name[$testkey]}"
done

..... and this should give the output Value3 then Value1


Is this what you need, or have I missed the point?


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash array

I'm trying to move files based on a certain day..my question is on array's, what's wrong with the way I'm stepping through the array? what i have gives me syntax errors..any ideas...so far I have #! /bin/bash sun_d=`cal |grep -v |awk '{printf " " $1}' ` echo "$sun_d" ls -l... (1 Reply)
Discussion started by: gubten
1 Replies

2. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

3. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

4. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

5. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

6. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

7. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

8. Shell Programming and Scripting

Array in Bash!

sorry I am new to the bash scripting since I was used to c shell programming. I am writting a script in bash for which I have to create an array in bash. I was able to do parse one variable through command line by doing": Lets say the below code is part of temp.bash and is called like: temp.bash... (5 Replies)
Discussion started by: dixits
5 Replies

9. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

10. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies
orddict(3erl)						     Erlang Module Definition						     orddict(3erl)

NAME
orddict - Key-Value Dictionary as Ordered List DESCRIPTION
Orddict implements a Key - Value dictionary. An orddict is a representation of a dictionary, where a list of pairs is used to store the keys and values. The list is ordered after the keys. This module provides exactly the same interface as the module dict but with a defined representation. One difference is that while dict considers two keys as different if they do not match ( =:= ), this module considers two keys as different if and only if they do not com- pare equal ( == ). DATA TYPES
ordered_dictionary() as returned by new/0 EXPORTS
append(Key, Value, Orddict1) -> Orddict2 Types Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function appends a new Value to the current list of values associated with Key . An exception is generated if the initial value associated with Key is not a list of values. append_list(Key, ValList, Orddict1) -> Orddict2 Types ValList = [Value] Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function appends a list of values ValList to the current list of values associated with Key . An exception is generated if the initial value associated with Key is not a list of values. erase(Key, Orddict1) -> Orddict2 Types Key = term() Orddict1 = Orddict2 = ordered_dictionary() This function erases all items with a given key from a dictionary. fetch(Key, Orddict) -> Value Types Key = Value = term() Orddict = ordered_dictionary() This function returns the value associated with Key in the dictionary Orddict . fetch assumes that the Key is present in the dictio- nary and an exception is generated if Key is not in the dictionary. fetch_keys(Orddict) -> Keys Types Orddict = ordered_dictionary() Keys = [term()] This function returns a list of all keys in the dictionary. filter(Pred, Orddict1) -> Orddict2 Types Pred = fun(Key, Value) -> bool() Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() Orddict2 is a dictionary of all keys and values in Orddict1 for which Pred(Key, Value) is true . find(Key, Orddict) -> {ok, Value} | error Types Key = Value = term() Orddict = ordered_dictionary() This function searches for a key in a dictionary. Returns {ok, Value} where Value is the value associated with Key , or error if the key is not present in the dictionary. fold(Fun, Acc0, Orddict) -> Acc1 Types Fun = fun(Key, Value, AccIn) -> AccOut Key = Value = term() Acc0 = Acc1 = AccIn = AccOut = term() Orddict = ordered_dictionary() Calls Fun on successive keys and values of Orddict together with an extra argument Acc (short for accumulator). Fun must return a new accumulator which is passed to the next call. Acc0 is returned if the list is empty. The evaluation order is undefined. from_list(List) -> Orddict Types List = [{Key, Value}] Orddict = ordered_dictionary() This function converts the Key - Value list List to a dictionary. is_key(Key, Orddict) -> bool() Types Key = term() Orddict = ordered_dictionary() This function tests if Key is contained in the dictionary Orddict . map(Fun, Orddict1) -> Orddict2 Types Fun = fun(Key, Value1) -> Value2 Key = Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() map calls Func on successive keys and values of Orddict to return a new value for each key. The evaluation order is undefined. merge(Fun, Orddict1, Orddict2) -> Orddict3 Types Fun = fun(Key, Value1, Value2) -> Value Key = Value1 = Value2 = Value3 = term() Orddict1 = Orddict2 = Orddict3 = ordered_dictionary() merge merges two dictionaries, Orddict1 and Orddict2 , to create a new dictionary. All the Key - Value pairs from both dictionaries are included in the new dictionary. If a key occurs in both dictionaries then Fun is called with the key and both values to return a new value. merge could be defined as: merge(Fun, D1, D2) -> fold(fun (K, V1, D) -> update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D) end, D2, D1). but is faster. new() -> ordered_dictionary() This function creates a new dictionary. size(Orddict) -> int() Types Orddict = ordered_dictionary() Returns the number of elements in an Orddict . store(Key, Value, Orddict1) -> Orddict2 Types Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function stores a Key - Value pair in a dictionary. If the Key already exists in Orddict1 , the associated value is replaced by Value . to_list(Orddict) -> List Types Orddict = ordered_dictionary() List = [{Key, Value}] This function converts the dictionary to a list representation. update(Key, Fun, Orddict1) -> Orddict2 Types Key = term() Fun = fun(Value1) -> Value2 Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() Update a value in a dictionary by calling Fun on the value to get a new value. An exception is generated if Key is not present in the dictionary. update(Key, Fun, Initial, Orddict1) -> Orddict2 Types Key = Initial = term() Fun = fun(Value1) -> Value2 Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() Update a value in a dictionary by calling Fun on the value to get a new value. If Key is not present in the dictionary then Initial will be stored as the first value. For example append/3 could be defined as: append(Key, Val, D) -> update(Key, fun (Old) -> Old ++ [Val] end, [Val], D). update_counter(Key, Increment, Orddict1) -> Orddict2 Types Key = term() Increment = number() Orddict1 = Orddict2 = ordered_dictionary() Add Increment to the value associated with Key and store this value. If Key is not present in the dictionary then Increment will be stored as the first value. This could be defined as: update_counter(Key, Incr, D) -> update(Key, fun (Old) -> Old + Incr end, Incr, D). but is faster. NOTES
The functions append and append_list are included so we can store keyed values in a list accumulator . For example: > D0 = orddict:new(), D1 = orddict:store(files, [], D0), D2 = orddict:append(files, f1, D1), D3 = orddict:append(files, f2, D2), D4 = orddict:append(files, f3, D3), orddict:fetch(files, D4). [f1,f2,f3] This saves the trouble of first fetching a keyed value, appending a new value to the list of stored values, and storing the result. The function fetch should be used if the key is known to be in the dictionary, otherwise find . SEE ALSO
dict(3erl) , gb_trees(3erl) Ericsson AB stdlib 1.17.3 orddict(3erl)
All times are GMT -4. The time now is 02:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy