Unix - Need a double incremental loop - Unix

This is a discussion on Unix - Need a double incremental loop - Unix ; 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 ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Unix - Need a double incremental loop

  1. Unix - Need a double incremental loop

    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.


  2. Re: Unix - Need a double incremental loop

    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


  3. Re: Unix - Need a double incremental loop

    In article <1134492174.602565.40410@g49g2000cwa.googlegroups.c om>,
    "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.


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

  4. Re: Unix - Need a double incremental loop

    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


  5. Re: Unix - Need a double incremental loop

    Thobias Vakayil wrote:
    > 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
    >


    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

  6. Re: Unix - Need a double incremental loop

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


  7. Re: Unix - Need a double incremental loop

    In article <1134572155.495994.155980@g43g2000cwa.googlegroups. com>,
    "AMBROZE" 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

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

+ Reply to Thread