affect a exploded a string into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting affect a exploded a string into an array
# 1  
Old 04-05-2007
assign a exploded a string to an array

I would like to affect an exploded string into an array.
Code:
one:two::four

into an array:
Code:
a[0] => one
a[1] => two
a[2] =>
a[3] => four

Quite simple in other languages with functions like explode() or split().

The best I could come up with was this:
Code:
until [ "$token" =  "$string" ]
do
        token=${string%%:*}    # takes the first token
        a[$i]=$token           # affects the token to the array element
        string=${string#*:}    # chops that token from the string
        ((i++))
done

This return what I was looking for but I find it quite heavy for something basic.

I also tried:
Code:
a=($(echo $string | nawk -F":" '{$1=$1; print}'))

But this skips the empty tokens as it returns
Code:
a[0] => one
a[1] => two
a[2] => four

Anything more simple for such a basic thing?

Last edited by ripat; 04-06-2007 at 08:20 AM..
# 2  
Old 04-05-2007
Using IFS

HTML Code:
#!/bin/ksh
Str=one:two::four
IFS=:
set -A Test $Str
let count=${#Test[@]}
let i=0
while [[ $i -lt $count ]]; do
print "Test[$i] ==> ${Test[$i]}"
((i=i+1))
done
Please check if this solves the problem,

Thanks
Nagarajan Ganesan
# 3  
Old 04-05-2007
Quote:
Originally Posted by ripat
I would like to affect an exploded string into an array.
Code:
one:two::four

into an array:
Code:
a[0] => one
a[1] => two
a[2] =>
a[3] => four

Quite simple in other languages with functions like explode() or split().

The best I could come up with was this:
Code:
until [ "$token" =  "$string" ]
do
        token=${string%%:*}    # takes the first token
        a[$i]=$token           # affects the token to the array element
        string=${string#*:}    # chops that token from the string
        ((i++))
done

This return what I was looking for but I find it quite heavy for something basic.

I also tried:
Code:
a=($(echo $string | nawk -F":" '{$1=$1; print}'))

But this skips the empty tokens as it returns
Code:
a[0] => one
a[1] => two
a[2] => four

Anything more simple for such a basic thing?
set -A arr `echo "one:two::four" | awk -F":" '{for(i=1;i<=NF;i++) print $i}'`
echo "${arr[0]}"
and so on.
# 4  
Old 04-05-2007
@ennstate
Works fine in ksh but not in bash (I should have mentioned I was looking for a bash solution). Bash doesn't support the -A option for the set built in command. The only way I found to assign values to an array is array=(one two tree). And doing IFS=:; array=(one:two::four) doesn't work either.

@Deal_NoDeal
Also skips the empty fields as it returns
a[0] => one
a[1] => two
a[2] => four

instead of

a[0] => one
a[1] => two
a[2] =>
a[3] => four

Still trying.
# 5  
Old 04-05-2007
Quote:
Originally Posted by ripat
I would like to affect an exploded string into an array.
Code:
one:two::four

into an array:
Code:
a[0] => one
a[1] => two
a[2] =>
a[3] => four

Quite simple in other languages with functions like explode() or split().

The best I could come up with was this:
Code:
until [ "$token" =  "$string" ]
do
        token=${string%%:*}    # takes the first token
        a[$i]=$token           # affects the token to the array element
        string=${string#*:}    # chops that token from the string
        ((i++))
done

This return what I was looking for but I find it quite heavy for something basic.

I also tried:
Code:
a=($(echo $string | nawk -F":" '{$1=$1; print}'))

But this skips the empty tokens as it returns
Code:
a[0] => one
a[1] => two
a[2] => four

Anything more simple for such a basic thing?

In bash or ksh93:

Code:
string=one:two::four
oldIFS=$IFS
IFS=:
a=( $string )
IFS=$oldIFS

In any POSIX shell:

Code:
string=one:two::four
unset a
while :
do
  case $string in
     *:*) a[${#a[@]}]=${string%%:*}
             string=${string#*:}
             ;;
     *) a[${#a[@]}]=${string%%:*}
         break ;;
  esac
done


# 6  
Old 04-06-2007
Quote:
Originally Posted by cfajohnson
Code:
string=one:two::four
oldIFS=$IFS
IFS=:
a=( $string )
IFS=$oldIFS

Works fine.

I did try changing the value of IFS but not correctly. Strange that you can assign values by doing:
Code:
a=(one two three)

But the following does not work
Code:
IFS=:
a=(one:two:three)

You have to first put the string in a variable as you did. Thank you.
# 7  
Old 04-06-2007
Quote:
Originally Posted by ripat
Works fine.

I did try changing the value of IFS but not correctly. Strange that you can assign values by doing:
Code:
a=(one two three)

But the following does not work
Code:
IFS=:
a=(one:two:three)

You have to first put the string in a variable as you did. Thank you.

If you are writing the literal values, there's no reason to use a colon:

Code:
a=( one two three )

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. UNIX and Linux Applications

Having too many connections could affect performance ?

Good evening, i need your help please I will try to describe the scenario briefly: In a Telecom Production system application receives a certain files called CDRs(call detail records) to be processed by doing some operating systems operations and then database operations like creating indexes... (4 Replies)
Discussion started by: alexcol
4 Replies

3. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. UNIX for Dummies Questions & Answers

.profile - changes don't affect the login

I have modified the .profile in my profile and I don't see any effect. Why the changes don't have effect? I tried both on the account at a server where I have limited permissions as user and to my local pc (as user). Is it the .profile overridden by some other file? It looks weird because I... (8 Replies)
Discussion started by: myle
8 Replies

6. Solaris

Will Password change affect authorized_keys?

Hello gurus, I have question. I have enabled ssh on the servers. I am planning to change the oracle user's(os user) password. If I change the password will it affect the authorized_keys? Do I need to regenerate it. Thanks, (2 Replies)
Discussion started by: oracleuser
2 Replies

7. Shell Programming and Scripting

affect variable

hi , i want to store variable but i get always error when i excute this command ligne : var = awk '{print $1}' file1 echo $var how can i store the var? thanks (7 Replies)
Discussion started by: kamel.seg
7 Replies

8. Solaris

making new default gateway take affect

How do you make the changes take affect when you change the subnet masks or default router without rebooting? (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. UNIX for Advanced & Expert Users

changig uid and the affect it will have

Hello, I have 2 sun servers. We are running Oracle apps. We have to apply patches to both servers whenever a patch needs to be applied for Oracle. We use the same username eg. applmgr on both servers. We have a shared patch area which we can run the patch from on both boxes. the uid for... (2 Replies)
Discussion started by: d__browne
2 Replies

10. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: sumitgarg
6 Replies
Login or Register to Ask a Question