Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load array with set -A problem 1

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
Im trying to load an array with set -A but it is space delimited, and I am using strings witn spaces. Does anyone know how to keep the string intact for the array using set -A?
 
And what about proper quoting ?
Can you please post the code and the strings values and the expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
welp, if I do something like this:

Code:
#!/usr/bin/ksh

set -A services "'string 1' 'string 2' 'string 3'" 


for i in $services
 do
   echo $i
 done

It outputs this:

'string
1'
'string
2'
'string
3'

It still wants to separate by spaces, no matter how I quote the entries. Ideas?
 
With my ksh:
set -A services "'string 1' 'string 2' 'string 3'"
will populate the array with ONE element
set -A services 'string 1' 'string 2' 'string 3'
will populate the array with THREE elements

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
when I populate it like this:

set -A 'string 1' 'string 2'

it only recognizes the first two words (inside the quotes) and runing the nprogram displays this:

String
1
 
For i in $services
I don't think it's the right way to browe an array.
Try something like this:
set -A services 'string 1' 'string 2' 'string 3'
i=0
while [ $i -lt ${#services[*]} ]; do
echo ${services[$i]}; ((i+=1))
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
#!/bin/ksh

s1='string 1'
s2='string 2'

IFS='
'
set -A services ${s1} ${s2}

typeset -i i=0
while (( i < ${#services[*]} ))
do
   print "i->[${i}] services->[${services[$i]}]"
   (( i=i+1 ))
done

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top