Home » Category » Linux & Unix

Linux & Unix: Unix - Need a double incremental loop

212| Tue, 20 May 2008 10:56:00 GMT| ambroze| Comments (6)
I need a counter that will increment and create output something like
this:

1 - 1
1 - 2
1 - 3
2 - 1
2 - 2
2 - 3
3 - 1
3 - 2
3 - 3

Need to set the first columns to a variable.

Keywords & Tags: unix, double, incremental, loop, linux

URL: http://software.itags.org/linux-unix/608808/
 
«« Prev - Next »» 6 helpful answers below.
On 13 Dec 2005 08:42:54 -0800, AMBROZE wrote:
> I need a counter that will increment and create output something like
> this:
> 1 - 1
> 1 - 2
> 1 - 3
> 2 - 1
> 2 - 2
> 2 - 3
> 3 - 1
> 3 - 2
> 3 - 3
> Need to set the first columns to a variable.


Options:
1 Two loops should do it
2 Use two variables. Reset var 2 when var 2 = 4

http://tldp.org/LDP/abs/html/index.html

bittwister | Tue, 20 May 2008 10:57:00 GMT |

In article <1134492174.602565.40410...g49g2000cwa.googlegroups.com>,
"AMBROZE" <ambroze_ebay...comcast.net> wrote:

> I need a counter that will increment and create output something like
> this:
> 1 - 1
> 1 - 2
> 1 - 3
> 2 - 1
> 2 - 2
> 2 - 3
> 3 - 1
> 3 - 2
> 3 - 3
> Need to set the first columns to a variable.


Pseudo-code:

for i from 1 to 3
for j from 1 to 3
print i "-" j
end
end

Translating this into whatever language you're using is a simple
exercise for the reader.

Isn't this something from about week 4 or 5 of a beginning programming
class?
Barry Margolin, barmar...alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

barry_margolin | Tue, 20 May 2008 10:58:00 GMT |

AMBROZE wrote:

>I need a counter that will increment and create output something like
>this:
>1 - 1
>1 - 2
>1 - 3
>2 - 1
>2 - 2
>2 - 3
>3 - 1
>3 - 2
>3 - 3
>Need to set the first columns to a variable.
>
>

shell script :

#!/usr/bin/ksh
i=1
while [ "$i" -le "3" ]
do
j=1
while [ "$j" -le "3" ]
do
echo "$i - $j"
j=`expr $j + 1`
done
i=`expr $i + 1`
done

Regards,

Thobias Vakayil

thobias_vakayil | Tue, 20 May 2008 11:00:00 GMT |

Thobias Vakayil wrote:
> AMBROZE wrote:
>
> shell script :
> #!/usr/bin/ksh
> i=1
> while [ "$i" -le "3" ]
> do
> j=1
> while [ "$j" -le "3" ]
> do
> echo "$i - $j"
> j=`expr $j + 1`
> done
> i=`expr $i + 1`
> done
>
> Regards,
> Thobias Vakayil
>


In ksh (normally #!/bin/ksh)
you better avoid the time-consuming `expr`:

j=$((j+1))
i=$((i+1))

and you can further embedd the incrementation:

i=0
while [ $((i+=1)) -le 3 ]
do
j=0
while [ $((j+=1)) -le 3 ]
do
echo "$i - $j"
done
done

In this case two for loops are most efficient
for both sh and ksh:

for i in 1 2 3
do
for j in 1 2 3
do
echo "$i - $j"
done
done
Michael Tosch ... hp : com

michael_tosch | Tue, 20 May 2008 11:00:00 GMT |

Yes it might be - but isn't this fun? Not sure we need extra comments
like this please.

ambroze | Tue, 20 May 2008 11:02:00 GMT |

In article <1134572155.495994.155980...g43g2000cwa.googlegroups.com>,
"AMBROZE" <ambroze_ebay...comcast.net> wrote:

> Yes it might be - but isn't this fun? Not sure we need extra comments
> like this please.


This reply makes no sense without context.

Since you use Google, please see <http://cfaj.freeshell.org/google/>
Barry Margolin, barmar...alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

barry_margolin | Tue, 20 May 2008 11:03:00 GMT |

Linux & Unix Hot Answers

Linux & Unix New questions

Linux & Unix Related Categories