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!

Adding text strings as array items 1

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB

Im running a csh under Linux (so nothing that normally with csh under Solaris) and have hit a snag. How do I append a text string onto an array?

If I do...

set T = ()
set T = ( $T "My String" "plus a bit more" )

I want "My string" to be one element and "plus a bit more" to be the next. In practice the array takes each space delimited word (so I get "My", "String", "plus" etc.).

Any ideas? Ive tried every combination of brackets, quotes, curlies.




 
theres apparently a few bugs with arrays in csh, personally I stick to bash anyway.


bash answer:

[root@ ~]# fred=("123" "456" "789")
[root@ ~]# echo ${fred[0]}
123

[root@ ~]# echo ${fred[1]}
456

[root@ ~]# echo ${fred[2]}
789

[root@ ~]# fred[3]="this is a string"
[root@ ~]# echo ${fred[3]}
this is a string

[root@ ~]# fred[1]="changed to a longer string"
[root@ ~]# echo ${fred[1]}
changed to a longer string



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 

Thanks for that.

There are so many bugs with csh I think you're right about switching to bash.
 
missed a trick also, but you probably know this:

echo ${#fred[@]}
returns no of items already in the array.... useful when knowing what to add next.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I found something new to me today, I'll add it here as its relative
If you are adding filenames to the array instead of text, eg joe.bloggs.doc
fred[6]=joe.bloggs.doc

echo ${fred[6]##*.}
returns doc


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
additional

fred[6]=joe.bloggs.doc

echo ${fred[6]%.*}

returns joe.bloggs


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top