Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to manipulate more than one variables in a loop

Status
Not open for further replies.

huangwason

Programmer
Oct 10, 2006
21
DE
for instance, print out something like:

400 202 402 204 404 206 ...

what I am thinking is use one variable manipulates digits begin with 4, another variable for digits begin with 2.
for ((i=0,j=1;i<=2,j<=3;i++,j++));do
printf "%s %s" 400+$i*2 200+$j*2
done

the problem is that it looks like i and j are always in the same length, the above code can not output like

400 202 402 204 404 206 208 210 212

any proposal? thanks first




 
Something like this ?
Code:
awk 'BEGIN{for(i=0;i<=9;++i)printf "%s %s ",400+i*2,202+i*2;printf "\n"}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks PHV, but it does not match my requirment, I can not obtain the output such as
400 202 402 204 404 206 208 210 212
 
Hi

Note that the second parameter must be a single expression. And using the needless multiplication just slows it down.
Code:
for ((i=400,j=202;i<=404||j<=206;i+=2,j+=2)); do
  printf "%s %s " $i $j
done
To solve your problem I see no nice solution, only this :
Code:
for ((i=400,j=202;i<=404||j<=212;i+=2,j+=2)); do
  [ $i -le 404 ] && printf "%s " $i
  [ $j -le 212 ] && printf "%s " $j
done

Feherke.
 
thanks guys, my original intention is to out put something like
430 432 434 436 002 430 432 434 436 004 430 432 434 436 006 430 432 434 436 008 430 432 434 436 00a 430 432 434 436 00c ...

I found a easy way only use one variable in the for loop, and print the second variable when it accumulate. Thanks again. A piece of code looks like

let m=1
for ((j=1;j<=$16;j++)); do
for ((k=0;k<4;k++)); do
printf "%x " 4$(echo "(48+$i*2)+$k*2" | bc)
done
[ $m -le 195 ] && printf "%x " $m
let m+=2
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top