Assigning String to Arrays


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Assigning String to Arrays
# 1  
Old 06-15-2010
Assigning String to Arrays

Hello everybody, this is my first post here, I think IŽll be here for a long time Smilie

I wanted to know how I can assign each single character from a string to an array...

For example, "unix"
I have to set,
Code:
array[0]=u
array[1]=n
array[2]=i
array[3]=x

However, that string may change in each execution, so my script should do it by itself

IŽve been trying to do it for a long time but I canŽt find the way

Thanks!

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-17-2010 at 02:41 AM.. Reason: code tags
# 2  
Old 06-16-2010
Code:
#!/bin/sh

declare -a arr
len=`expr \`echo $var | wc -c\` - 1`
for i in $(seq 1 $len)
do
   arr[`expr $i - 1`]=$(echo $var | awk -vj=$i '{ printf substr($0, j, 1)}')
done

echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
echo ${arr[3]}

I hope there would be more elegant solution to this..
# 3  
Old 06-16-2010
Try (bash/ksh93):
Code:
str=unix
eval array=($(echo $str | awk '$1=$1' FS=))

or
Code:
eval array=($(sed 's/./ &/g'<<<$str))

Output:
Code:
$ echo ${array[1]}
n


Last edited by Scrutinizer; 06-16-2010 at 06:41 AM..
# 4  
Old 06-16-2010
Thanks!
it worked
# 5  
Old 06-17-2010
Code:
# a=unix;ix=0; for i in `fold -w1 <(echo $a)`; do array[ix]=$i; echo "$ix. variable of array->" ${array[ix]}; let ix=$ix+1; done
0. variable of array-> u
1. variable of array-> n
2. variable of array-> i
3. variable of array-> x

# 6  
Old 12-03-2010
Code:
for i in $(seq 1 `echo ${#var}`)
do
     arr[`expr $i - 1`]=$(echo "$var" | awk -v i=$i '{print substr($0, i, 1)}')
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

2. Shell Programming and Scripting

Script stops running after assigning empty string for a variable

Hi, This is the first time I see something like this, and I don't why it happens. Please give me some help. I am really appreciate it. Basically I am trying to remove all empty lines of an input.. #!/bin/bash set -e set -x str1=`echo -e "\nhaha" | grep -v ^$` #str2=`echo -e "\n" |... (4 Replies)
Discussion started by: yoyomano
4 Replies

3. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

4. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. Shell Programming and Scripting

Problems assigning a string to a variable

Hello everyone, My problem looks quite simple , how to assign a string with spaces and lines "\n" to a variable. I've tried all kind of quoting and is impossible, bash always try to execute the string and never executes a simple assignation. This is part of the code ... (1 Reply)
Discussion started by: trutoman
1 Replies

7. Shell Programming and Scripting

Sub-string extraction on arrays

Hi, I'm trying to extract the middle of an array that is of variable length but always has a first and last common element, The following works OK... #!/bin/bash ARRAY='switch' ARRAY='option1' ARRAY='option2' ARRAY='option3' ARRAY='value' SWITCH=${ARRAY:0:1} VALUE=${ARRAY:(-1)}... (1 Reply)
Discussion started by: ASGR
1 Replies

8. Shell Programming and Scripting

retreiving and assigning values and manipulating string in a for loop

Hi I am new to shell scripting and i am preparing a script. for now i am work on a sub part of it..but i am unable to make it work. --- the test code that i am working on -------------------------- IFS="" Sample_eve=`psg proc_s | grep tY` n=0 for line in $Sample_eve... (41 Replies)
Discussion started by: Anteus
41 Replies

9. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

10. Shell Programming and Scripting

Another 10 second question. Assigning 2 strings to one string.

Hi, I am trying to combine 2 strings into one new string. I know there are existing threads on this topic, but I am having troubles. The variables have variables within their names which is causing me problems. Bad subsitution is the error. The problem line is in red below. thanks (again) to... (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question