The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:35 AM
How to read from txt file and use that as an array pinky UNIX for Dummies Questions & Answers 4 10-08-2007 12:18 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 03:02 PM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-29-2006 02:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-23-2008
aoussenko aoussenko is offline
Registered User
  
 

Join Date: May 2008
Posts: 119
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
  #2 (permalink)  
Old 07-23-2008
thana thana is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 55
There should be some form of seperator among the values
  #3 (permalink)  
Old 07-23-2008
Sivaswami's Avatar
Sivaswami Sivaswami is offline
Registered User
  
 

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

cut -c <position>
  #4 (permalink)  
Old 07-23-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
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
  #5 (permalink)  
Old 07-24-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,367
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 12:16 PM.. Reason: add awk solution
  #6 (permalink)  
Old 07-24-2008
BMDan BMDan is offline
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 02:09 PM.. Reason: Add sed solution
  #7 (permalink)  
Old 07-24-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
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
Sponsored Links
Closed Thread

Bookmarks

Tags
shell array, variable manipulation

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0