ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a non-contiguous array where the array index describes a numeric representation of the corresponding server which is stored in the array at that position.
I have a loop to traverse through the array and I would like for it to print out at what index that server was located, but I can't find out how to do this. A simplified example:
Code:
db[2]="server1"
db[7]="server2"
db[347]="server3"
db[888]="server4"
lookingFor="server3"
for server in ${db[@]}
do
if [ $server -eq $lookingFor ]
then
index="???" #How do I access the array index??
echo "$server found at index $index"
fi
done
Last edited by Jus144tice; 10-21-2006 at 07:17 AM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
#!/bin/bash
db[2]="server1"
db[7]="server2"
db[347]="server3"
db[888]="server4"
lookingFor="server3"
for server in ${db[@]} ; do
let index=$index+1
if [ $server == $lookingFor ] ; then
echo "$server found at index$index"
fi
done
Yes, that will work, however, it is horribly inefficient since I need to traverse 888 values for i, when the size of the array is only 4. If that is the only way to do it, then there should not even be non-contiguous arrays in the language. Here's what I want to do in php:
Code:
$db[2]="server1"
$db[7]="server2"
$db[347]="server3"
$db[888]="server4"
$lookingFor="server3"
for ($db as $index => $server)
{
if ($server == $lookingFor)
{
echo "$server found at index $index\n"
}
}
Basically, I want to be able to treat the array as a hash table and access the key and the element at each position of the table.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
The array is contiguous, but the index is not. As a matter of fact, the index can be anything, and "4" is exactly as valid as a fifth element as is "87634786".
The mistake (if I may call it like this, it is not really a mistake) you make is that you use integers to traverse up to the value of 888.
If you really want to use indices like this, I would recommend to create a second array containing a hash table:
hash[0]=2
hash[1]=7
hash[2]=347
hash[3]=888
I don't know how you obtained values like "347" and "888" etc, but it should be possible to put those values in the hash array. Now you can traverse the hash array instead from 0 -> 3, and use the values to index your original array.
The thought had occurred to me to use two arrays, one for the index (really server ID number) and other for server name. I may end up doing that if I can't find a way to do what I really want. I'm not convinced that the arrays are contiguous but the indices are not. Take this, for example:
This prints out Array size = 4. If what you say was true, you would think that the previous code would print Array size = 889. That's why I still believe that if the language is smart enough to handle non-contiguous array indices and still know how many elements are in the array (I'm assuming it doesn't traverse through all possible indices and look for non-null values), then it must be smart enough to tell me what the index (or key) is for the value I am using.
Of course, I could be wrong and that's just the way the language works. But if anyone has a solution or even further clarification, please post it. Thanks.
Maybe you should look at it like this.
A pure array eg in C or Bash, works purely on integer element indexes and you can't easily go straight to a given element unless you already know the index value.
In PHP, the arrays are really implented as hashes, but you can also use numeric indexes and treat them as arrays.
FYI, in Perl you have arrays and hashes as (different) basic data types anyway.
HTH
I know that many years have passed, but since the OP's original question hasn't yet been given a succinct, to-the-point answer I will present a solution here.
The trick is to use an exclamation mark in front of the array variable name to get the corresponding array of indices:
Code:
echo ${!db[@]}
This produces the following output:
2 7 347 888
The complete code answering the OP question becomes:
Code:
#! /bin/bash
db[2]="server1"
db[7]="server2"
db[347]="server3"
db[888]="server4"
lookingFor="server3"
for i in ${!db[@]}
do
if [ "${db[$i]}" = "$lookingFor" ]
then
echo "${db[$i]} found at index $i"
fi
done
This program loops only through the four defined indices and produces the output
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.