Programming :: Bash Array - More Than One Value Per Item?
Nov 28, 2010
Is it possible to have an array in Bash that can hold more than one value per item?
For example I would like an array like this:
Entry 1: apple, green
Entry 2: banana, yellow
And be able to call the fruit names and their colour in a list. Something like:
for fruit in "${array[@]}"
do
echo a $fruit is $colour
done
If that is possible, is there a limit to values per item? For example some entries in an array could be:
Entry 1: apple, green, round, pips, tree
Entry 2: banana, yellow, long, skin, tree
And I would like to pick out the values such as #3 being "round" and "long".
View 3 Replies
ADVERTISEMENT
Jun 20, 2010
I looked on the net for such function or example and didin't find anything, thus after having made one i guess it would be legitimate to drop it to see what others thinks of it.
#!/bin/bash
addelementtoarray()
{
local arrayname=$1
[code]....
View 10 Replies
View Related
May 13, 2010
I have this code:
Code:
original_content=${content[@]};
echo ${#content[@]} # is 23
echo ${#original_content[@]} # is 1
I'd expect original_content should be copy of content. How to copy array?
View 2 Replies
View Related
Oct 14, 2010
I am using gnu bash 3.2I need to split the string into array like
a=this_is_whole_world.file # split [_.]
I need to split this on _ and . some thing like this
a[0]=this
a[1]=is
a[2]=whole
a[3]=world
a[4]=file
preferable using bash regex. if not sed is also ok.
View 2 Replies
View Related
Mar 29, 2011
I'm trying my hand at arrays in bash for a backup script. Now I not sure if this is the correct thing to do and just look at website and amended but does'nt work. Could someone tell me where I'm going wrong
LOG_DIR="/home/ops/Desktop/temp"
array[0]=3
array[1]=/home/ops/Desktop/dir1
[code]...
View 8 Replies
View Related
Sep 24, 2010
Code:
test=(1 2 3 4 5)
for car in ${test[@]}
do
echo "Element : $car"
[code]....
if variable $car equals 1, new element is added "6" to an array. But i don't know why when i am printing all elements of this array (echo "Element : $car") this element ("6") is not mentioned, but if i make a command which check an amount of contained elements by array it will be 6 elements.
View 14 Replies
View Related
May 19, 2011
I have the following function that does not iterate through the array I want to be able to do some manipulation on each element in the array[@].it appears the below array has only one item in the array whereas i want the array to have 3 items hence the loop three times printing the message Any ideas why this is not happening ?
function foo() {
name =$1
array=( "$2" )
[code]...
View 5 Replies
View Related
May 18, 2010
I want to list the contents of a zip file amd put each entry into an array. I've been doing
[code]....
View 4 Replies
View Related
Apr 30, 2010
...and returning the index of the found element in its array.
I have:
for ((i=0; i < ${#array1[@]}; i++)); do
# Read each line of the file test
if [[ $(eval "sed -n '$(($i+1))'p test") == *${array2[0]}* ]]
stuff
I want to find the index of the found substring in array2 and only if it isn't found, move on to the next element of array2. I don't know the size of array2 so that [0] has just got to go.
View 14 Replies
View Related
Sep 18, 2010
I have a file (called twitterstatus.tmp) that looks like this:
Code:
<status>
<id>24854489768</id>
<text>Are we gonna ride the sun home?</text>
<id>55266987</id>
[code].....
How could I feed this into an array, with each element containing everything between the <status> </status> tags?
View 9 Replies
View Related
May 26, 2011
I need to know how to assign a result from a select. I am clueless on the sytax. I am trying this in bash. Maybe I am not assigning the array right. It gives me the whole row in the echo instead of just field a. How do I get fieldA = a in the select. Note script was stripped for security on database info but the syntax is same.
Code:
#!/bin/sh
results="$(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse 'select a,b,c,d from tblMytable')"
for rows in "${results[@]}"
do
fieldA=${rows[0]};
echo ${fieldA};
done
View 2 Replies
View Related
Jun 27, 2011
Code:
#loop until there are no more files
while [ ${#files[@]} -gt 0 ]; do
for num in `seq 1 ${#files[@]}`; do
wurd=`tail -4 ${files[$num-1]}`
[code].....
Everything works.. but when it has to unset.. it breaks.. it doesnt give an error though.. it just jams the program.
View 7 Replies
View Related
Jan 6, 2010
I am trying to do this:
1. Read csv delimited file line1 and store all values in array
2. Use the values stored in the array and replace values in other text file with them
3. read line2 in the cvs file and repeat the process
4. Do above for all lines in the cvs file
for example:
file1.cvs content:
text1,text2,text3,"text 4"
a1,a2,3,"a 4"
file.txt content:
some text $array1$ some text
some text $array2$ some text
1. read line 1 - text1,text2,text3,"text 4" put each value in array X[] lines that contain spaces in cvs will have double quotes
2. read x[1] and replace value $array1$ (in file.txt) with x[1]read x[2] and replace value $array2$ (in file.txt) with x[2] and so on
Can above be accomplished in BASH and how?
View 1 Replies
View Related
Oct 13, 2010
This may be a basic bash array/string operation related question, but I couldn't find any direct answer. So here it goes:I have a lot of data sorted in various directories. All directories need same processing except for a special group of directories. I have a symbolic link of the script in discussion in each directory. I want the script to get the name of the current directory, check if that belongs to special group and do specific operations.So I get the name of the directory
Code:
mm=`basename `pwd``
Now the the group of directories that needs something different to be done, contains these
[code]...
View 4 Replies
View Related
Dec 1, 2010
I would like to read unix file permissions into a bash array for processing but tbh I have no idea how to do this. Then I will check for each individual access right l, d, x etc.
View 11 Replies
View Related
Jan 26, 2011
I have a command that outputs n lines of text, and I want to place each line into an array element, but I can't seem to get the syntax correct
So my command is this:
cat $configfile | sed -n '/cluster:'$clustername'/,/cluster/ p' | awk /host/
Which produces many lines depending on the value of $clustername. I'd like to get each line as elements of an array.
View 5 Replies
View Related
May 29, 2011
PI'm trying to write a script to list all open ports in the MINIUNPND chain in iptables and use the procotol, port and destination ip to open ports on another router using upnpc.Here is the output of iptables -L MINIUPNPD
Code:
>iptables -L MINIUPNPD
Chain MINIUPNPD (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:19955
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:20054
ACCEPT tcp -- anywhere 192.168.3.130 udp dpt:10654
ACCEPT tcp -- anywhere 192.168.3.121 tcp dpt:29955
code....
No matter what i do i cant seem to remove the first 4 characters from the MYPROT array to leave only the digits. Also i cant seem to read the array back???
I thought it would simply be a loop reading each line and passing the fields in variables, executing upnpc commands i need then moving to the next line of the file until it reached the EOF.
View 12 Replies
View Related
Jan 5, 2011
I have been trying to write a simple snip of bash shell code to import from 1 to 100 records into a Bash array.
I have a CSV file that is structured like:
record1,item1,item2,item3,item4
record2,item1,item2,item3,item4
record3,item1,item2,item3,item4
record4,item1,item2,item3,item4
And would like to get this data into corresponding arrays as such:
$record1[item1-4]
$record2[item1-4]
$record3[item1-4]
$record4[item1-4]
View 9 Replies
View Related
Jun 4, 2010
This _almost_ works. I just can't quite get it to honor the spaces.
Code:
#!/bin/bash
profiles=(
PROFILE_ONE
)
PROFILE_ONE=(setting1 "setting number 2")
[code].....
View 9 Replies
View Related
Dec 2, 2010
I have an array called arrayini which stores numbers. I want to take log to the base 2 of each of the numbers in that array and put it in file called result. I've used the following code to do it.
Code:
size=${#arrayini[@]}
for ((i=0;i<size;i++))
do
echo "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l
done >result
It works fine but its taking pretty long to calculate since I've got about 230,000 items in the array. So I decided to store the result into an array hoping that it'd be faster. I tried the following code. arrayresult is where I try and store the result. The code doesn't work because of the second last line.
Code:
unset arrayresult
size=${#arrayini[@]}
for ((i=0;i<size;i++))
do
arrayresult[$i]="scale = 12; l(${arrayini[$i]})/l(2)" | bc -l
done >FILE2
There is a syntax error clearly.
View 6 Replies
View Related
Mar 6, 2011
I am trying to dynamically delete and add information into the array "blah"
Code:
int blahsize = 1;
char** blah = (char**) calloc(blahsize+1,sizeof(char*));
Adding information:
Code:
blah[1]=stuff1;
blah[2]=stuff2;
code....
View 2 Replies
View Related
Jun 7, 2010
I have trouble converting a short array to a char array
Code:
short pShort[4] = { 0x41, 0x42, 0x43, 0x44 };
How to convert this to a char array?
View 4 Replies
View Related
Aug 9, 2009
I'm writing a PHP program. I've encountered a problem; in the following code I try to pass $_POST['delete'] which is an array as the value of a hidden input to some form, but it doesn't do so.there's something wrong with converting PHP array into HTML array. I'm sure that $_POST['delete'] is not null and is a real array.
echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';
View 4 Replies
View Related
May 26, 2010
(I am using vector() and matrix() functions from "Numerical recipes in C".)There are 100 numbers to be stored in 2D array of 10 rows and 10 columns.100 numbers are stored in a 1D array.I get "segmentation fault" at the line indicated in the segment of my code below:
Code:
:
:
#define size 100
#define nl 1
[code]....
View 12 Replies
View Related
Apr 13, 2010
I have my own declared types and I have to operate with lists of items. I wish to write a one macro, instead of using a function for each type. It's intended to be like this: Code: #define ____append_to_list(head,
item, type)
type* ptr;
for ( ptr = head; ptr->next != NULL; ptr = ptr->next )
[code].....
View 9 Replies
View Related
Jul 14, 2010
I was reading some code the other day and thought that I saw they were using the list.pop method to pop of more than one item at a time. Something like this.
Code:
l = [ 'a', 'b', 'c', 'd', 'e' ]
l.pop(0:3)
['a','b','c']
l
['d','e']
But of course when I looked at invoking the method, it only works for one item at a time. I'm working on a data structure where I'm parcing weather data that is 15 items long that are return in as data in a list that has 3,000 + items. I was wanting to pop off a lists of the 1st 15 items breaking them into chuncks (still trying to figure out how I'm going to store the associations as each day has 2-4 readings an hour for the past 72 hours, so I was wanting to group the days together but have each data point for each time).
I know I can just write a function (with some additional tweaking needed for neg starts to work on the right end):
Code:
def longpop (seq, length=1, start=0):
slice = seq[start:start+length]
del seq[start:start+length]
return slice
But with increasing data sizes, I was wondering if there was a faster built-in way of doing this (perhaps implemented under the hood written originally in C as an extention, for example).
View 6 Replies
View Related
May 4, 2011
I need to make a quick gui to interface with another application. I'm using Java swing to produce it, however have no experience with JAVA. I have used net bean to produce the gui. At one part I have a jList private javax.swing.JList jSymbolList1;
I want on a button press to add or change members of the list from a text field. I thought I could so this simply by using the addelement eg jSymbolList1.addElement("hello world"); However this produces an error. Can anyone tell me how to add a string or how to change a string at a set position?
View 4 Replies
View Related
Nov 24, 2010
i'm using awk inside bash. i've got an array in awk called arrayinawk. everytime i call another awk command in bash i have to keep creating arrayinawk to work with it. is there anyway i can store arrayinawk in bash and just call the stored value next time i use awk?
View 14 Replies
View Related
Dec 2, 2010
I have a file named file.txt with the following contents
Code:
19 man
24 house
44 dyam
90 random
I want to read the file into array and store each line in each index. I've tried using the following code.
Code:
dataarray=($( < file.txt ))
It stores each word in each index rather than each line in each index.
View 3 Replies
View Related
May 1, 2011
I am trying to execute a .c program of mine through bash... Problem is it takes two arguments (files) and am trying to use "for" loops to do it. I use the for loops to take the files (same number in both directories) and put them into arrays. I use the command "ls -1" in the "for loop" so they are sorted in such a way that they are in the same time and date order upon when the program is executed.
Here is the code:
Code:
for a in `ls -1 /analyses/data1/*_prt.txt`;
do echo $a;
done
[code]....
HOWEVER, $a in this case does not change with the loop. Thus, the program doesn't work. I then tried:
Code:
for a in `ls -1 /analyses/data1/*_prt.txt`;
for sfc in `ls -1 /analyses/data2/*_ht.txt`;
do
./plot_data $a $b >> log_file.log;
done
But that produces the error: syntax error near unexpected token `for'. As a "do" statement must come after the for loop call.
View 13 Replies
View Related