How to declare an array in UNIX and print the elements with tab delimits?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to declare an array in UNIX and print the elements with tab delimits?
# 1  
Old 04-15-2015
Question How to declare an array in UNIX and print the elements with tab delimits?

Hello,

In a shell script, I want to declare an array and subsequently print the elements with tab delimits.

My array has the following structure and arbitrary elements:
Code:
myArray=('fgh' 'ijk' 'xyz' 'abc');

Next, I would like to print it with a '\n' at the end.

Thanks for your input!

~Guss
# 2  
Old 04-15-2015
With bash, try this:
Code:
IFS=$'\t';  printf "%s\n" "${myArray[*]}"
fgh    ijk    xyz    abc

Please note that you'll need to save IFS before and restore it later.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-15-2015
It looks like you are using bash - the array declaration is correct.
The the full syntax is:
Code:
declare -a myArray=( value1 value2 value3 )

Code:
for i in ${myArray[*]}; do  printf "%s\t" $i; done

Note this works correctly for your example -- if your array elements have tabs or spaces in them it will not work

in that case something like this using very basic syntax:
Code:
cnt=0
while [  $cnt -lt ${#myArray[*]} ]
do
     printf "%s\t"  "${myArray[i]}"
     cnt=$(( $cnt + 1  ))
done
echo ""

Note: RudiC's note is more elegant - but this is the stuff you need to understand about iterating through an array.
This User Gave Thanks to jim mcnamara For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (3 Replies)
Discussion started by: spflanze
3 Replies

2. Shell Programming and Scripting

Select / Print odd-even elements of an array

Hello experts, I wish to print the contents of odd-even numbered indices of an array. The problem statement is as follows : 1. The first line contains an integer, (the number of test cases). 2. Each line of the subsequent lines containing a string. Example: 2 Haider Bash ... (4 Replies)
Discussion started by: H squared
4 Replies

3. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

4. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

5. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

6. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

7. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

8. Shell Programming and Scripting

How to declare an array to take more than 10,000 characters

Hi Guys Need some help I am reading the string values from the text files into the shell script and had them feed into array I have declared an associative array as TYPE t_user_id_tab IS TABLE OF VARCHAR2(3000);\n my_user_id t_user_id_tab;\n varchar2 is limiting me to take only... (0 Replies)
Discussion started by: pinky
0 Replies

9. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies

10. Shell Programming and Scripting

declare, assign variables using array, counter, loop

using bash Tru64... converting DCL to shell... any tips to make this work would be greatly appreciated. Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call... (3 Replies)
Discussion started by: egkumpe
3 Replies
Login or Register to Ask a Question