The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read csv into Hash array? kinmak Shell Programming and Scripting 1 05-07-2008 07:35 AM
How to read from txt file and use that as an array pinky UNIX for Dummies Questions & Answers 4 10-07-2007 09:18 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 05:42 AM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 12:02 PM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-28-2006 11:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-23-2008
Registered User
 

Join Date: May 2008
Posts: 44
how to read a var value into array

Hi
I need to read a value of the variable into array so each character/digit will become an array element,for example:
A=147921231432545436547568678679870
The resulting array should hold each digit as an element.
Thanks a lot for any help -A
Reply With Quote
Forum Sponsor
  #2  
Old 07-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 52
There should be some form of seperator among the values
Reply With Quote
  #3  
Old 07-23-2008
Sivaswami's Avatar
Registered User
 

Join Date: Mar 2007
Location: India
Posts: 58
You can use cut command inside a loop.

cut -c <position>
Reply With Quote
  #4  
Old 07-23-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,027
Yes, that would be the 1st step.

Write it for example like
Code:
A="842 5 2 64  24 11"
You can then cycle through it with for example
Code:
for ELE in ${A}; do
   echo "I want ${ELE] cookies!"
done
Reply With Quote
  #5  
Old 07-24-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 736
Quote:
Originally Posted by aoussenko View Post
Hi
I need to read a value of the variable into array so each character/digit will become an array element,for example:
A=147921231432545436547568678679870
The resulting array should hold each digit as an element.
Thanks a lot for any help -A
Next time please use code tags.
Here is the bash solution:
Code:
$ A=147921231432545436547568678679870
$ set -- $(for i in $(seq 0 $((${#A} - 1)));do printf "%s " ${A:$i:1};done)
$ echo $*
1 4 7 9 2 1 2 3 1 4 3 2 5 4 5 4 3 6 5 4 7 5 6 8 6 7 8 6 7 9 8 7 0
... or awk solution:
Code:
set -- $(awk -v v="$A" 'BEGIN{split(v,a,"");for (i=1;i<= length(v);i++) printf "%s ",a[i]}')

Last edited by danmero; 07-24-2008 at 09:16 AM. Reason: add awk solution
Reply With Quote
  #6  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 66
I like this a little better than danmero's example, as it actually puts it in an array:

Code:
for i in $(seq 0 $((${#string}-1))); do array[$i]=${string:$i:1}; done
Which produces:

Code:
$ A=147921231432545436547568678679870; for i in $(seq 0 $((${#A}-1))); do array[$i]=${A:$i:1}; done

$ set | grep array
array=([0]="1" [1]="4" [2]="7" [3]="9" [4]="2" [5]="1" [6]="2" [7]="3" [8]="1" [9]="4" [10]="3" [11]="2" [12]="5" [13]="4" [14]="5" [15]="4" [16]="3" [17]="6" [18]="5" [19]="4" [20]="7" [21]="5" [22]="6" [23]="8" [24]="6" [25]="7" [26]="8" [27]="6" [28]="7" [29]="9" [30]="8" [31]="7" [32]="0" [33]="")
Note that this will fail for especially-large strings; just break out of the for and use a while (or a C-style for()) instead.

If that's what you're looking for, you can also create the same effect as danmero's script with sed:
Code:
$ echo 147921231432545436547568678679870 | sed 's/\(.\)/\1 /g'
1 4 7 9 2 1 2 3 1 4 3 2 5 4 5 4 3 6 5 4 7 5 6 8 6 7 8 6 7 9 8 7 0

Last edited by BMDan; 07-24-2008 at 11:09 AM. Reason: Add sed solution
Reply With Quote
  #7  
Old 07-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,913
With Z-Shell:

Code:
zsh-4.3.4% A=147921231432545436547568678679870
zsh-4.3.4% print $A[4]
9
zsh-4.3.4% print $A[-3]
8
With bash/ksh93, here string and fold:

Code:
$ a=($(fold -w1<<<$A))
$ printf "%s\n" "${a[0]}"
1
$ printf "%s\n" "${a[3]}"
9
For older shells:

Code:
$ A=147921231432545436547568678679870
$ set -- `printf "%s\n" "$A"|fold -w1`
$ printf "%s\n" "$1"
1
$ printf "%s\n" "$4"
9
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
shell array, variable manipulation

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0