string to array

Hi Experts,
I'm new to Shell Scripting world and need a little help from you "Scripting Gurus".

I want to convert a string (entered by a user) to array in csh. I want something like this:

echo "Enter the Numbers: "
set num = $<

Suppose, user enters:

1 2 3 4 5 6 7

This value is stored in variable num as string "1 2 3 4 5 6 7".
I want this string to be converted into a array with seven values. :confused:
like num[1] = 1, num[2] = 2, ....

This may be damn easy for you people. :smiley:

Any help in this regard is highly appreciated.

Thanks in advance! :slight_smile:

Regards,

Sumit Garg

PS: I've implemented the whole idea using loops, but I was particularly looking for some easier approach.

Must it really be done in csh (take a look at this)? This kind of thing is really easy to do with perl

#!/usr/bin/perl

chomp( $num = <STDIN> );

@num_array = split(" ",$num);

# Print the third element, for example...
print "$num_array[2]\n";

Cheers
ZB

one more way ...

echo "Read input"
read num
set -A arr $num
echo ${arr[0]}
echo ${arr[*]}


Thanks a lot.
But yes, I was specifically looking for some way in csh.
I know it's very easy in perl.

I think we can use "cut" command in csh to solve the purpose.
Anybody know about the exact syntax?

Regards.

Sumit

Hi All,
Thanks a lot for your help.

I was able to do so by cut command. :stuck_out_tongue:

[b]Million thanks to this forum. :slight_smile:

Thanks,

Sumit

If you must use csh, then something like this should work (note, I'm using tcsh here...)

#!/bin/tcsh

echo "Please enter your numbers:"
set num = $<:q
set array = `echo $num`

#print an element
echo $array[2]

Also, please ensure that you read the forum rules. Duplicate and cross-posting is NOT permitted. I have deleted your duplicate thread from the Shell Scripting forum.

Thanks,
ZB

Hi ZB,

Cool Man!!! :cool:

That was easy, thanks a lot.

Regarding the duplicate mails, I'll keep that in mind next time.

Thanks,

Sumit Garg