Unix questions - Unix - Need a double incremental loop

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > December 2005 > Unix - Need a double incremental loop





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Unix - Need a double incremental loop
AMBROZE

2005-12-13, 5:58 pm

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.

Bit Twister

2005-12-13, 5:58 pm

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

Barry Margolin

2005-12-13, 8:50 pm

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 ***
Thobias Vakayil

2005-12-14, 2:50 am

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

Michael Tosch

2005-12-14, 7:50 am

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
AMBROZE

2005-12-14, 5:57 pm

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

Barry Margolin

2005-12-15, 2:51 am

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 ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com