Parsing String.
Hello All,
I have a case, wherein I have a string of the format "attr1=value1 attr2=value2 attr3=value3 attr4=value4" How do I extract the value associated with for a given attributename. For eg. I need to get a value of "value2" when I give an input for attribute name as "attr2". Note, each attribute values is space seperated and value can contain / or , or - as part of it. Thanks Sunil Kumar |
What programming language do you want to use?
May be the sscanf function in C |
The language choice is useful here.
But, I'll add my two cents anyway. In python, you can split the string (based on " ") and feed that into a list comprehension (splitting the resulting strings based on "=") with an output to the dict function. This can be done in 1 line and gives a dictionary with values indexed by attributes. This works with any number of key=value pairs in the string. |
One way is to use an associative array if your shell supports the concept.
Code:
#!/usr/bin/ksh93 Code:
aArray[attr1] is value1 |
Perl; use 'split()' fn.
|
some other solutions here
|
No rule against asking across different sites, is there?
|
Quote:
|
Thanks for your replies.
|
All times are GMT -5. The time now is 04:26 PM. |