how to read a var value into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to read a var value into array
# 1  
Old 07-23-2008
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  
Old 07-23-2008
There should be some form of seperator among the values
# 3  
Old 07-23-2008
You can use cut command inside a loop.

cut -c <position>
# 4  
Old 07-23-2008
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  
Old 07-24-2008
Quote:
Originally Posted by aoussenko
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 01:16 PM.. Reason: add awk solution
# 6  
Old 07-24-2008
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 03:09 PM.. Reason: Add sed solution
# 7  
Old 07-24-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ how would you read this file into an array.

here is the pesudo file. REREREEEEEERRRREER SOMEStrinG1234 RERRRR EEERRRREER SOMEStrinG1224 REREREEEREERRR REE SOMEStrinG1214 REREREREREREREEEER SOMEStrinG1204 RERE EEEEEERRRRRRR SOMEStrinG1294 REREEREEE ERRRREER SOMEStrinG1284 REREREEEEEERR REER here is my attempted code #include... (3 Replies)
Discussion started by: briandanielz
3 Replies

2. Shell Programming and Scripting

[SOLVED] Read html attachment from /var/mail

Hi, I am receiving an email to the server with a html attachment. When I check the /var/mail the attachment is showing as junk characters. I want to move that html file to a particular location in that server. Please help me. Regards Neethu (4 Replies)
Discussion started by: Neethu
4 Replies

3. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 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. Programming

assign array with a local var???

Hi all, I am just wondering is this (below code) valid in programming int xx = 10110; int i; long yy = {1,2,3,4,5,6,7,8,xx}; I compiled this using gcc will all working option successfully without getting any warning or error. I didn't see this kind of code in my past... (7 Replies)
Discussion started by: zing_foru
7 Replies

6. UNIX for Advanced & Expert Users

assign array with a local var???

topic moved to programming (0 Replies)
Discussion started by: zing_foru
0 Replies

7. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

8. Solaris

urgent: single-user-mode, /var/tmp read-only

this is the situation: Power outage. Root mirror (svm). it goes to single-user-mode, asking for fsck. Fsck suceeds for one disk, but fail for the other. I can't use vi-editor, it says /var/tmp/Xz12a is a read-only file system. I need to break the mirror, there's no copy of... (2 Replies)
Discussion started by: Sun Fire
2 Replies

9. Shell Programming and Scripting

Read array from a file

Hi I've a config file like: file1 #comment k_array: 1 2 3 4 5 n_array: 7 8 9 0 11 I'd like to write a script that read it and store k_array and n_array in 2 arrays. I mean the script should be able to use both as array. I've tried to use awk as (only for one array): ... (5 Replies)
Discussion started by: Dedalus
5 Replies

10. UNIX for Dummies Questions & Answers

Tell UW-Imap to read /var/mail/mailbox ??

Hi, I've got UW-IMAP installed on Gentoo. I'm using EXIM as the transfer agent. EXIM is working fine (I think)... all incoming mail - external and internal goes to /var/mail/username file. I can verifiy this by reading this file directly with nano. Also, IMAPd is running. It lets me login... (2 Replies)
Discussion started by: d11wtq
2 Replies
Login or Register to Ask a Question