Re: Stupid programming question

Re: Stupid programming question

am 19.05.2004 18:45:38 von salt

#!/usr/bin/perl

open(FILE1, "file1") or die "cannot open file 1\n";
open(FILE2, "file2") or die "cannot open file 2\n";

while ($n1 = and $n2 = )
{
chomp $n1;
chomp $n2;
$product = $n1 * $n2;
print "$product\n";
}
}


--
Rudy
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Stupid programming question

am 19.05.2004 19:06:18 von Michael French

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:

SUM=0
NUMBER1=`cat $FILE1`
for m in $NUMBER1
do
SUM=`echo "scale=4; $SUM * $m" |bc`
done

Not exactly right, but you get the jist of it. Obviously this won't work:

SUM=0
NUMBER1=`cat $FILE1`
NUMBER2=`cat $FILE2`
for m in $NUMBER1 and n in $NUMBER2
do
SUM=`echo "scale=4; $m * $n" |bc`
done

Thanks for any help you can provide, let me know if you need more info.

Michael French
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

RE: Stupid programming question

am 19.05.2004 19:22:04 von jj2195

You could read one file into an shell array then do a loop read of the
second file (like in you example) multiplying against the appropriate
array slot.

You could grab a single number from a file with sed.
Count=$( grep -c '$' file1 )
i=1
while read Num1
do
Num2=$( sed -n $i file2 )
Mul=$(( $Num1 * $Num2 ))
...
i=$(( i + 1 ))
done < file1

You can use $Count to check if you've gone beyond the end of file2.

John Julian


-----Original Message-----
From: linux-admin-owner@vger.kernel.org
[mailto:linux-admin-owner@vger.kernel.org] On Behalf Of Michael French
Sent: Wednesday, May 19, 2004 1:06 PM
To: linux-admin@vger.kernel.org
Subject: Stupid programming question


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:

SUM=0
NUMBER1=`cat $FILE1`
for m in $NUMBER1
do
SUM=`echo "scale=4; $SUM * $m" |bc`
done

Not exactly right, but you get the jist of it. Obviously this won't
work:

SUM=0
NUMBER1=`cat $FILE1`
NUMBER2=`cat $FILE2`
for m in $NUMBER1 and n in $NUMBER2
do
SUM=`echo "scale=4; $m * $n" |bc`
done

Thanks for any help you can provide, let me know if you need more info.

Michael French
-
To unsubscribe from this list: send the line "unsubscribe linux-admin"
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid programming question

am 20.05.2004 00:03:02 von Russell Evans

On Wed, 19 May 2004 12:06:18 -0500
"Michael French" 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
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html