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
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
Array with special Characters donaldfung UNIX for Dummies Questions & Answers 1 06-08-2008 12:18 PM
2D Array to display characters Raynon Shell Programming and Scripting 16 05-15-2008 06:38 AM
How to declare an array to take more than 10,000 characters pinky Shell Programming and Scripting 0 01-15-2008 10:38 PM
How to echo password characters mukluk UNIX for Dummies Questions & Answers 4 09-12-2005 07:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2007
rorey_breaker rorey_breaker is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 20
create array holding characters from sring then echo array.

Hi,

I wish to store $string1 in $string1array[] a character in each array element.
Then i wish to echo the entire array to the screen so that it reads as the normal string again.

I have been trying with the code below but does not work. Please help...

To put string into array:

Code:
#!/bin/bash
length=$ ## Int length of string1
 noletter=1
 cnt=1
 x=0
while [ $noletter -le $length ]
do
  set -- $( echo $string1 | cut -c${cnt} )
  string1array[$x]=$1
  noletter=$(( noletter + 1 ))
  cnt=$(( cnt + 1 ))
  x=$(( $x + 1 ))
done
to echo array:

Code:
y=0
length = ##integer length of word
echo "Array is: "
while [ $y -lt $length ]
do
  echo -ne "${string1array[$y]} "
  y=$(( y + 1 ))
done
  #2 (permalink)  
Old 09-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
With zsh:
Code:
{ string=abcd
        for i ({1..$#string}) print -n $string[i]
print;}
with recent bash versions:

Code:
{ string=abcd
i=0
while ((i<${#string}));do
	printf "%s " "${string:i:1}"
	((++i))
done
printf "\n";}
  #3 (permalink)  
Old 09-28-2007
rorey_breaker rorey_breaker is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 20
Is this printing the array called string ?
  #4 (permalink)  
Old 09-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
It's printing the characters one by one ...

Last edited by radoulov; 09-28-2007 at 08:32 AM..
  #5 (permalink)  
Old 09-28-2007
rorey_breaker rorey_breaker is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 20
I am needing to have the characters stored in an array, so that I can compare them easily with a single character.
  #6 (permalink)  
Old 09-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
That can be done with code above too ...
Sponsored Links
Closed Thread

Bookmarks

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 04:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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