linux-admin.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Russell Evans" <russell-evans@qwest.net>
To: linux-admin@vger.kernel.org
Subject: Re: Stupid programming question
Date: Wed, 19 May 2004 15:03:02 -0700	[thread overview]
Message-ID: <20040519150302.21cec504@downtown> (raw)
In-Reply-To: <40AB940A.3080601@ashevillemail.com>

On Wed, 19 May 2004 12:06:18 -0500
"Michael French" <mfrench@ashevillemail.com> wrote:

>     I have two text files each with a single column of numbers in them
>     
> and I want to write a script that multiplies the number on each line
> of file one with the corresponding number from file two and produces a
> sum of the multiplied numbers in the 2 files..  How would I do this
> with a loop?  This is just a small shell script, I can use perl if I
> need to, I just need to know the data structure.  If it was just one
> file, I would do something like this:

Putting everything into arrays makes it easy to use the values of the
data and results if you need to do something with them later.

Thank you
Russell

#!/bin/sh

### Variables ###

# First file containing column of numbers
FILE1=/path/file1
# Number of column in first file that contains numbers to be used
COLUMN1=1

# Second file containing column of numbers
FILE2=/path/file2
# Number of column in second file that contains numbers to be used
COLUMN2=1

### Declare Arrays ###
declare -a NUMBER_FILE1
declare -a NUMBER_FILE2
declare -a PRODUCT 

### FUNCTIONS ###
MULTIPLY () { 
 # Pull a column out of two files and multiple
 # Populate arrays with data and result
 # 
 if [ -r $FILE1 ] ; then 
   NUMBER_FILE1=(`awk '{print $COLUMN1}' $FILE1`)
   if [ -r $FILE2 ] ; then
     NUMBER_FILE2=(`awk '{print $COLUMN2}' $FILE2`)
     if [ ${#NUMBER_FILE1[@]} = ${#NUMBER_FILE2[@]} ] ; then
       for (( x=0 ; x<${#NUMBER_FILE1[@]} ; x++ )) ; do
         let PRODUCT[$x]=${NUMBER_FILE1[$x]}*${NUMBER_FILE2[$x]}
       done
     else
       echo "Variance in data structure"
       exit 1
     fi
   else
     echo "$FIL2 is not readable"
   fi
 else
   echo "$FILE1 is not readable"
 fi
} # End MULTIPLY

### MAIN ###
MULTIPLY
echo ${PRODUCT[@]}
for (( y=0 ; y<${#PRODUCT[@]} ; y++ )) ; do
  echo "${NUMBER_FILE1[$y]}*${NUMBER_FILE2[$y]}=${PRODUCT[$y]}"
done 
exit 0

  reply	other threads:[~2004-05-19 22:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-19 17:06 Stupid programming question Michael French
2004-05-19 22:03 ` Russell Evans [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-05-19 17:22 JULIAN, JOHN C (AIT)
2004-05-19 16:45 ` A. R. Vener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040519150302.21cec504@downtown \
    --to=russell-evans@qwest.net \
    --cc=linux-admin@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).