LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 12-02-2010, 11:41 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Bash: Calculating in array and storing result in array


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. Please help.

Last edited by ghantauke; 12-02-2010 at 11:43 AM.
 
Old 12-02-2010, 12:12 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
You need to set it up as an embedded command.
Code:
arrayresult[$i]=$( echo "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l )
This puts the output of the string into the array variable.

Edit: Also, don't you need to echo the string into bc?

By the way, you can output a list of an array's index numbers with ${!array[@]}. It can be convenient and more accurate when looping through all of an array's elements.
Code:
for i in ${!arrayini[@]}
do
arrayresult[$i]=$( echo "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l )
done
Edit again: Directing the output to FILE2 is useless here, since there is no output from this loop. It just sets the array. You'd have to echo it separately. Try this after the loop:
Code:
printf "%s\n" ${arrayresult[@]} >FILE2

Last edited by David the H.; 12-02-2010 at 12:29 PM. Reason: As stated
 
Old 12-02-2010, 12:17 PM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by David the H. View Post
You need to set it up as an embedded command.
Code:
arrayresult[$i]=$( "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l )
This puts the output of the string into the array variable.

By the way, you can output a list of an array's index numbers with ${!array[@]}. It can be convenient and more accurate when looping through all of an array's elements.
Code:
for i in ${!arrayini[@]}
do
arrayresult[$i]=$( "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l )
done >FILE2
Thanks for telling me about the ${!arrayini[@]} looping. And thanks for the great solution.

Last edited by ghantauke; 12-02-2010 at 12:33 PM.
 
Old 12-02-2010, 12:17 PM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,159

Rep: Reputation: 258Reputation: 258Reputation: 258
Hi

I think what takes most of the time is calling bc 230000 times. Why not call it once instead? Also l(2) can be computed once instead of 230000 times.

Code:
#!/bin/bash

for (( i=1 ; i<=230000 ; i++ )) ; do
  a[$i]=$i
done

size=${#a[@]}

(
echo "scale=12; d=l(2);"
for (( i=1 ; i<=$size ; i++ )) ; do
  echo "l(${a[$i]})/d;"
done
) | bc -l
Edit:
I'm slow, answers came while i was typing.

Last edited by Guttorm; 12-02-2010 at 12:19 PM.
 
Old 12-02-2010, 12:27 PM   #5
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Nevermind.

Last edited by ghantauke; 12-02-2010 at 12:32 PM.
 
Old 12-02-2010, 12:42 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by Guttorm View Post
I'm slow, answers came while i was typing.
No problems there. I was focusing entirely on the syntax problems, because I have no clue as to the actual math. And actually, my "solution" is probably slower than the original code, because pretty much all of the same operations have to be done, and then some. Yours is probably much more efficient.
 
Old 12-02-2010, 01:28 PM   #7
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by Guttorm View Post
Hi

I think what takes most of the time is calling bc 230000 times. Why not call it once instead? Also l(2) can be computed once instead of 230000 times.

Code:
#!/bin/bash

for (( i=1 ; i<=230000 ; i++ )) ; do
  a[$i]=$i
done

size=${#a[@]}

(
echo "scale=12; d=l(2);"
for (( i=1 ; i<=$size ; i++ )) ; do
  echo "l(${a[$i]})/d;"
done
) | bc -l
Edit:
I'm slow, answers came while i was typing.
Holy Molly! You're absolutely right. Its like 20 times faster now. Thanks a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] storing array from awk in bash ghantauke Linux - Newbie 20 11-25-2010 10:59 AM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 09:11 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 11:07 AM
Storing the result of execvp to a char array in C kponenation Programming 3 12-14-2005 06:45 PM


All times are GMT -5. The time now is 04:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration