KSH: Accessing individual words in a variable string (like arrays) - Unix

This is a discussion on KSH: Accessing individual words in a variable string (like arrays) - Unix ; Kindly suggest the ksh equivalent of the following csh script snippet ############################## set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson) echo $presidents[1] $presidents[4-7] ##############################...

+ Reply to Thread
Results 1 to 5 of 5

Thread: KSH: Accessing individual words in a variable string (like arrays)

  1. KSH: Accessing individual words in a variable string (like arrays)

    Kindly suggest the ksh equivalent of the following csh script snippet


    ##############################
    set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
    Jackson)

    echo $presidents[1] $presidents[4-7]
    ##############################

  2. Re: KSH: Accessing individual words in a variable string (likearrays)

    On 30 Nov., 02:38, use...@sta.samsung.com wrote:
    > Kindly suggest the ksh equivalent of the following csh script snippet
    >
    > ##############################
    > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
    > Jackson)
    >
    > echo $presidents[1] $presidents[4-7]
    > ##############################


    What version of ksh are you using? Try one of...

    set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    echo $1 $4 $5 $6 $7

    set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    printf "%s" $1; shift 3; echo "$@"


    set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    echo ${@:1:1} ${@:4:4}

    presidents=( Washington Adams Jefferson Madison Monroe Qunicy-Adams
    Jackson )
    echo ${presidents:1:1} ${presidents:4:4}


    Janis

  3. Re: KSH: Accessing individual words in a variable string (likearrays)

    On 30 Nov., 09:49, Janis wrote:
    > On 30 Nov., 02:38, use...@sta.samsung.com wrote:
    >
    > > Kindly suggest the ksh equivalent of the following csh script snippet

    >
    > > ##############################
    > > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
    > > Jackson)

    >
    > > echo $presidents[1] $presidents[4-7]
    > > ##############################

    >
    > What version of ksh are you using? Try one of...
    >
    > set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    > echo $1 $4 $5 $6 $7
    >
    > set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    > printf "%s" $1; shift 3; echo "$@"
    >
    > set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
    > echo ${@:1:1} ${@:4:4}
    >
    > presidents=( Washington Adams Jefferson Madison Monroe Qunicy-Adams
    > Jackson )
    > echo ${presidents:1:1} ${presidents:4:4}


    Oops. The latter should have been...

    echo ${presidents[@]:1:1} ${presidents[@]:4:4}

    >
    > Janis



  4. Re: KSH: Accessing individual words in a variable string (likearrays)

    On Nov 29, 7:38 pm, use...@sta.samsung.com wrote:
    > Kindly suggest the ksh equivalent of the following csh script snippet
    >
    > ##############################
    > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
    > Jackson)
    >
    > echo $presidents[1] $presidents[4-7]
    > ##############################




    The only thing that I can get to work is the following:

    presidents[0]=Washington
    presidents[1]=Adams
    presidents[2]=Jefferson
    presidents[3]=Madison
    presidents[4]=Monroe
    presidents[5]=Qunicy-Adams
    presidents[6]=Jackson

    echo ${presidents[0]} ${presidents[3]}
    ##### Could not find the KSH equivalent for echo
    $presidents[4-7]

    echo ${#presidents[*]}


    Is there some way to print a range of array elements?

    Thanks


    [Courtesy: http://www.livefirelabs.com/unix_tip...3/11102003.htm

    http://www.livefirelabs.com/unix_tip.../11172003.htm]


  5. Re: KSH: Accessing individual words in a variable string (likearrays)

    On 4 Dez., 01:52, Generic Usenet Account
    wrote:
    > On Nov 29, 7:38 pm, use...@sta.samsung.com wrote:
    >
    > > Kindly suggest the ksh equivalent of the following csh script snippet

    >
    > > ##############################
    > > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
    > > Jackson)

    >
    > > echo $presidents[1] $presidents[4-7]
    > > ##############################

    >
    > The only thing that I can get to work is the following:
    >
    > presidents[0]=Washington
    > presidents[1]=Adams
    > presidents[2]=Jefferson
    > presidents[3]=Madison
    > presidents[4]=Monroe
    > presidents[5]=Qunicy-Adams
    > presidents[6]=Jackson
    >
    > echo ${presidents[0]} ${presidents[3]}
    > ##### Could not find the KSH equivalent for echo
    > $presidents[4-7]
    >
    > echo ${#presidents[*]}
    >
    > Is there some way to print a range of array elements?


    See my reply upthread...

    In ksh93, bash, ... (otherwise use the 'set - ...' approach)

    echo ${presidents[@]:1:1} ${presidents[@]:4:4}

    The second second number defines the number of elements (if omitted
    until the end of the array list)... ${arrayvar[@]:index:length}

    Janis

    > Thanks
    >
    > [Courtesy:http://www.livefirelabs.com/unix_tip.../nov_2003/1110...
    >
    > http://www.livefirelabs.com/unix_tip..._2003/1117...]



+ Reply to Thread