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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split in cshell

Status
Not open for further replies.

guyleve

Programmer
Joined
Dec 23, 2002
Messages
1
Location
IL
Hi.
is it possible to split a word into a list containing it's
letters, for example can i split A12 into A 12 or A 1 2
please help.
 
Hi,
Does it have to be CSH? I mean it may be simpler in other shell languages like PERL.

Anyway in CSH to make a array of

@ x = 1
set a = "A12"
set b = `expr length $a`
set q = ""
while ( $x <= $b )
set q = ( $q `expr substr $a $x 1` )
@ x ++
end

echo &quot; q[1] == $q[1] &quot;
echo &quot; q[2] == $q[2] &quot;
echo &quot; q[3] == $q[3] &quot;

set q = ( $q `expr substr $a $x 1` )

q[1] == A
q[2] == 1
q[3] == 2

if you don't like.....

set q = ( $q `expr substr $a $x 1` )

you could use

set q = ( $q `echo $a | cut -c $x` )


now if you actually had a desire to put the first letter in one and the rest in another


set a = &quot;A12&quot;
set b = `echo $a | cut -c 1 `
set c = `echo $a | cut -c 2- `

echo &quot;b == $b&quot;
echo &quot;c == $c&quot;

b == A
c == 12

or you could make an array


set a = &quot;A12&quot;
set b = `echo $a | cut -c 1 `
set c = `echo $a | cut -c 2- `
set q = ( $b $c )
echo &quot; q[1] == $q[1] &quot;
echo &quot; q[2] == $q[2] &quot;

q[1] == A
q[2] == 12


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top