Unix Shell - Korn Shell IFS When Declaring An Array

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > Korn Shell IFS When Declaring An Array





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 Korn Shell IFS When Declaring An Array
absinth

2005-03-06, 8:47 pm

The standard Kornshell syntax for defining an array is
set -A arrayName elem1 elem2 elem3 elem4

Can I change the $IFS so I can pass in
set -A arrayName elem1,elem2,elem3,elem4

This is the code snippet:
I'm trying to do this in Kornshell using:

local oldIFS=$IFS
IFS=","
local newArray
set -A newArray `echo $1` [Which is one line from a CSV file]
IFS=$oldIFS

Currently the entire line is passed in as newArray[0].

I've also tried the following sed line:

Which I've bound to a splitFields function.

echo "${1}" | sed "s/^ */'/g;s/ *$/'/g;s/${2} */${2}/g;s/
*${2}/${2}/g;s/${2}/' '/g"

Which converts say "New York, Paris, London" to 'New York' 'Paris'
'London.'

But even if i pass this in as a parameter:
set -A newArray `splitFields "New York, Paris, London" ","`

Korn Shell ignores the quotes and reads the space so newArray[0]="New"
newArray[1]="York"

This won't work. Any suggestions?

Chris F.A. Johnson

2005-03-06, 8:47 pm

On Mon, 07 Mar 2005 at 01:28 GMT, absinth wrote:
> The standard Kornshell syntax for defining an array is
> set -A arrayName elem1 elem2 elem3 elem4
>
> Can I change the $IFS so I can pass in
> set -A arrayName elem1,elem2,elem3,elem4
>
> This is the code snippet:
> I'm trying to do this in Kornshell using:
>
> local oldIFS=$IFS
> IFS=","
> local newArray
> set -A newArray `echo $1` [Which is one line from a CSV file]


Why are you using echo?

set -A newArray $1

> IFS=$oldIFS
>
> Currently the entire line is passed in as newArray[0].
>
> I've also tried the following sed line:
>
> Which I've bound to a splitFields function.
>
> echo "${1}" | sed "s/^ */'/g;s/ *$/'/g;s/${2} */${2}/g;s/
> *${2}/${2}/g;s/${2}/' '/g"
>
> Which converts say "New York, Paris, London" to 'New York' 'Paris'
> 'London.'
>
> But even if i pass this in as a parameter:
> set -A newArray `splitFields "New York, Paris, London" ","`
>
> Korn Shell ignores the quotes and reads the space so newArray[0]="New"
> newArray[1]="York"
>
> This won't work. Any suggestions?


What version of ksh are you using? If it's ksh93, you can use:

newArray=( $1 )


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com