The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Cannot store integer value bennichan Shell Programming and Scripting 5 04-10-2008 11:20 AM
grep and store arunkumar_mca Shell Programming and Scripting 6 01-27-2006 06:31 AM
how can i store it ? shamal High Level Programming 2 12-08-2001 01:19 AM
How to store username dinplant Shell Programming and Scripting 3 11-13-2001 05:34 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 52
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 625
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 52
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 61
Stumble this Post!
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 10:57 AM.
Reply With Quote
  #5 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jun 2007
Posts: 377
Stumble this Post!
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";
Reply With Quote
  #6 (permalink)  
Old 07-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 52
Stumble this Post!
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"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0