Perl question - How do I print contents of an array on a single line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl question - How do I print contents of an array on a single line?
# 1  
Old 08-29-2011
Perl question - How do I print contents of an array on a single line?

I have the following code:

Code:
print @testarray;

which returns:
Code:
8
8
8
9

How do I return the array like this:
Code:
The output is: 8, 8, 8, 9

# 2  
Old 08-29-2011
Code:
print "The output is: ", (join ", ", @testarray), "\n";

Or
Code:
$"=", "; 
print "The output is: @testarray\n";


Last edited by yazu; 08-29-2011 at 02:11 PM..
# 3  
Old 08-29-2011
Code:
printf(("%s " x @testarray) . "\n", @testarray);

# 4  
Old 08-29-2011
Quote:
Originally Posted by yazu
Code:
print "The output is: ", (join ", ", @testarray), "\n";

Or
Code:
$"=", "; 
print "The output is: @testarray\n";

Hi Thanks for the help but I am getting the following output:

Code:
The output is: 8
, 8
, 8
, 9

# 5  
Old 08-29-2011
Try:
Code:
chomp @testarray;
$"=", "; 
print "The output is: @testarray\n";

# 6  
Old 08-29-2011
Quote:
Originally Posted by bartus11
Try:
Code:
chomp @testarray;
$"=", "; 
print "The output is: @testarray\n";

Yes, thank you! that worked! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display array contents on a new line

ksh eg arrayname=(1 2 3 4 5) I'm trying to display the individual contents of an array on a new line without using a loop, using one line of code. output 1 2 3 4 5 (3 Replies)
Discussion started by: squrcles
3 Replies

2. Shell Programming and Scripting

Perl: array name tha contains contents of a variable

Hi there I have a counter called my $counter = 0; I am trying to build an array that will have a name that is for example my @array0 = ("some", "stuff"); but instead of hard coding the "0" in the array name i want to use whatever value the aforementioned $counter has in it...so ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

3. Shell Programming and Scripting

Print array into a single file - AWK

Hi all, I been looking for a solution to the fact that when I use: for (i=1; i<=NF; i++) print $ifields that are originally in a single line are printed in a single line I have severals files for which the first 7 are the same, but the number of variables after that can vary, for example NF... (5 Replies)
Discussion started by: PaulaL
5 Replies

4. Shell Programming and Scripting

Joining contents in multiple lines to a single line

I do have a file with contents splited into multiple lines ADSLHLJASHGLJSKAGHJJGAJSLGAHLSGHSAKBV AJHALHALHGLAGLHGBJVFBJVLFDHADAH GFJAGJAGAJFGAKGAKGFAK AJHFAGAKAGAGKAKAKGKAGFGJDGDJJDGJDJDFAG ... ... .... 100's of lines I would like to rearrange the content of this file so it will be a... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

5. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

6. Shell Programming and Scripting

Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script. I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents. For example, I would like the output... (6 Replies)
Discussion started by: shwang3
6 Replies

7. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

8. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

9. Shell Programming and Scripting

ls command to print fifo of contents (or perl)

Hello, What are the options to print the contents of a directory in FIFO fashion? I have searched the forum for something relative to this question and found no answers. If there is a way please advise, if it has already been answered, please provide a link to the post. Alternately if... (1 Reply)
Discussion started by: jerardfjay
1 Replies
Login or Register to Ask a Question