store key value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting store key value
# 1  
Old 07-23-2008
store key value

Hi All,
I have a string like
echo $var
D_PROC=20080723 I_REPROC=N C_TYPE_FILE=INBOUND

Now I want it be stored in an associative array so that when we

echo $arr[D_PROC] it should be 20080723
# 2  
Old 07-23-2008
Question Are you saying D_PROC is the array element?

Normally, an array would have elements that are numbers. For example:
client[1]=Joe
client[2]=Kerry

So, you are trying to get to:
arr[D_PROC]=20080723
but, D_PROC is not representative of anything (it isn't a variable itself)

Please confirm.
# 3  
Old 07-23-2008
I want to store it in an associative array where subscripts may be a string eg
arr[first]=1
arr[second]=2
......

so when we echo $arr[first] , it should give me 1
# 4  
Old 07-23-2008
In PHP? No problem.
In Perl? Yep, we call those Hashes.
In very nearly any other language? Sure, with varying levels of straightforwardness.
In Bash? Not without some seriously-absurd buffoonery. It's almost always better to find another way to do it. Is this absolutely what you need, and no other approach will work?

Something like:
Code:
key="some_key" array[$(i=0; while [ x${arrayname[$i]} != x ]; do if [ x${arrayname[$i]} == x$key ]; then break; fi; export i=$(($i+1)); done; arrayname[$i]=$key; echo $i)]="value"

should work, but I kept running into scope issues with $arrayname. So:
Code:
key="some_key"; array[$(i=0; while [ x${arrayname[$i]} != x ]; do if [ x${arrayname[$i]} == x$key ]; then break; fi; export i=$(($i+1)); done; echo $i)]="value"
if [ x${arrayname[$i]} == x ]; then arrayname[$i]=$key; fi

Now, to retrieve, something like:
Code:
i=0; while [ x${arrayname[$i]} != x ]; do echo '$array['${arrayname[$i]}']="'${array[$i]}'"'; i=$(($i+1)); done

will retrieve all values, while
Code:
key="some_key"; i=0; while [ x${arrayname[$i]} != x ]; do if [ x${arrayname[$i]} == x$key ]; then echo ${array[$i]}; fi; i=$(($i+1)); done

will retrieve some_key.

Quite likely to break, full of holes, contains at least one bug, and generally inadvisable to use. Enjoy!

Last edited by BMDan; 07-23-2008 at 02:57 PM..
# 5  
Old 07-25-2008
use perl:

Code:
$str="D_PROC=20080723 I_REPROC=N C_TYPE_FILE=INBOUND";
$str=~s/=| /,/g;
@str=split(",",$str);
for($i=0;$i<=($#str+1)/2-1;$i++){
$hash{$str[$i*2]}=$str[$i*2+1];
}
print $hash{'I_REPROC'},"\n";

# 6  
Old 07-25-2008
How about if one of the value is null ie first_key=1 second_key= third_key=3.
Here the second_key is null.Is the perl script here going to read the values of second_key as " " or the string "third_key"
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

2. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

3. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

4. Cybersecurity

SSH key code versus server key code

Hi, When logging in using SSH access (to a remotely hosted account), I received a prompt to accept a server's key fingerprint. Wrote that string of code down for comparision. Already emailed my host for their listing of the string of code for the server's key fingerprint (for comparison,... (1 Reply)
Discussion started by: Texan
1 Replies

5. Shell Programming and Scripting

grep and store

When i grep a file and store it in a file it is storing as a zero byte file any way to avoid that..... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

6. Programming

how can i store it ?

how can i store a value which is larger than a long double value?? please help me.. -sham- (2 Replies)
Discussion started by: shamal
2 Replies
Login or Register to Ask a Question