Sponsored Content
Full Discussion: store key value
Top Forums Shell Programming and Scripting store key value Post 302217733 by BMDan on Wednesday 23rd of July 2008 11:52:53 AM
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..
 

6 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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
APC_ADD(3)								 1								APC_ADD(3)

apc_add - Cache a new variable in the data store

SYNOPSIS
bool apc_add (string $key, mixed $var, [int $ttl]) DESCRIPTION
array apc_add (array $values, [mixed $unused = NULL], [int $ttl]) Caches a variable in the data store, only if it's not already stored. Note Unlike many other mechanisms in PHP, variables stored using apc_add(3) will persist between requests (until the value is removed from the cache). PARAMETERS
o $key - Store the variable using this name. $keys are cache-unique, so attempting to use apc_add(3) to store data with a key that already exists will not overwrite the existing data, and will instead return FALSE. (This is the only difference between apc_add(3) and apc_store(3).) o $var - The variable to store o $ttl - Time To Live; store $var in the cache for $ttl seconds. After the $ttl has passed, the stored variable will be expunged from the cache (on the next request). If no $ttl is supplied (or if the $ttl is 0), the value will persist until it is removed from the cache manually, or otherwise fails to exist in the cache (clear, restart, etc.). o $values - Names in key, variables in value. RETURN VALUES
Returns TRUE if something has effectively been added into the cache, FALSE otherwise. Second syntax returns array with error keys. EXAMPLES
Example #1 A apc_add(3) example <?php $bar = 'BAR'; apc_add('foo', $bar); var_dump(apc_fetch('foo')); echo " "; $bar = 'NEVER GETS SET'; apc_add('foo', $bar); var_dump(apc_fetch('foo')); echo " "; ?> The above example will output: string(3) "BAR" string(3) "BAR" SEE ALSO
apc_store(3), apc_fetch(3), apc_delete(3). PHP Documentation Group APC_ADD(3)
All times are GMT -4. The time now is 07:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy