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]
##############################
Generic Usenet Accoun [ Fr, 30 November 2007 02:38 ] [ ID #1882411 ]

Re: KSH: Accessing individual words in a variable string (like

On 30 Nov., 02:38, use... [at] 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 "$ [at] "


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

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


Janis
Janis Papanagnou [ Fr, 30 November 2007 09:49 ] [ ID #1882420 ]

Re: KSH: Accessing individual words in a variable string (like

On 30 Nov., 09:49, Janis <janis_papanag... [at] hotmail.com> wrote:
> On 30 Nov., 02:38, use... [at] 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 "$ [at] "
>
> set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
> echo ${ [at] :1:1} ${ [at] :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[ [at] ]:1:1} ${presidents[ [at] ]:4:4}

>
> Janis
Janis Papanagnou [ Fr, 30 November 2007 10:27 ] [ ID #1882422 ]

Re: KSH: Accessing individual words in a variable string (like

On Nov 29, 7:38 pm, use... [at] 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_trick_shell_script/nov_ 2003/11102003.htm

http://www.livefirelabs.com/unix_tip_trick_shell_script/nov_ 2003/11172003.htm]
Generic Usenet Accoun [ Di, 04 Dezember 2007 01:52 ] [ ID #1885013 ]

Re: KSH: Accessing individual words in a variable string (like

On 4 Dez., 01:52, Generic Usenet Account <use... [at] sta.samsung.com>
wrote:
> On Nov 29, 7:38 pm, use... [at] 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[ [at] ]:1:1} ${presidents[ [at] ]:4:4}

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

Janis

> Thanks
>
> [Courtesy:http://www.livefirelabs.com/unix_tip_trick_shell_s cript/nov_2003/1110...
>
> http://www.livefirelabs.com/unix_tip_trick_shell_script/nov_ 2003/1117...]
Janis Papanagnou [ Di, 04 Dezember 2007 09:42 ] [ ID #1885022 ]
Linux » comp.unix.shell » KSH: Accessing individual words in a variable string (like arrays)

Vorheriges Thema: ssh login delays
Nächstes Thema: call c/c++ function from unix shell scripts