help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Is there a quick way to compute the ratio of two bash ar


From: Geir Hauge
Subject: Re: [Help-bash] Is there a quick way to compute the ratio of two bash arrays using bc?
Date: Sat, 8 Aug 2015 23:13:12 +0200
User-agent: Mutt/1.5.23 (2014-03-12)

On Sat, Aug 08, 2015 at 03:56:59PM -0500, Peng Yu wrote:
> Hi, I'd like to compute the ratios of two bash arrays. When there are
> just 2 elements in each array, I simply do the following as it is the
> quickest to type.
> 
> r=(
> $(bc -l <<< ${x[0]}/${y[0]})
> $(bc -l <<< ${x[1]}/${y[1]})
> )
> 
> When there are more elements, does anybody has a better way to do it?

Iterate the indices of one of the arrays:

    r=()
    for i in "address@hidden"; do
        r[i]=$(bc -l <<< "${x[i]}/${y[i]}")
    done

-- 
Geir Hauge



reply via email to

[Prev in Thread] Current Thread [Next in Thread]