The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 12 07-17-2008 01:38 PM
Problem In Formating Table as Output Lo11001 Shell Programming and Scripting 1 05-12-2008 03:35 AM
Output formating jaydeep_sadaria Shell Programming and Scripting 1 04-10-2008 09:39 AM
formating output Krrishv Shell Programming and Scripting 20 02-02-2007 05:39 AM
Formating cal output Krrishv Shell Programming and Scripting 2 01-11-2007 06:46 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2002
Registered User
 

Join Date: Jan 2002
Location: Houston
Posts: 11
formating array file output using perl

Hello,

I am trying to output the values in an array to a file. The output needs to be formated such that each array value is left jusified in a field 8 character spaces long. Also, no more than 6 fields on a line. For example:

@array= 1..14;

Needs to be output to the file like so:

1 2 3 4 5 6
7 8 9 10 11 12
13 14

Of course, the amount of array values will vary.
Can't this be done with the printf function?

Please Help,
seismic_willy
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-13-2002
Jimbo
Guest
 

Posts: n/a
Well, here's a while command that uses printf to output a unix array, and it closes out the line after every 6th entry:

Code:
#!/bin/ksh

a[1]=this
a[2]=array
a[3]=is
a[4]=holding
a[5]=nine
a[6]=entries
a[7]=right
a[8]=now
a[9]=.

i=1
w=0
while [ "${a[$i]}" ]
do
if [ $w -eq 6 ] ; then
   printf "\n"
   w=0
fi
printf "%-8s" ${a[$i]}
((i=i+1))
((w=w+1))
done

printf "\n"
exit 0
Reply With Quote
  #3 (permalink)  
Old 03-22-2002
Registered User
 

Join Date: Mar 2002
Posts: 2
Smile

Hey thanks for that question and i got my answer also
Reply With Quote
  #4 (permalink)  
Old 03-21-2007
Registered User
 

Join Date: Mar 2007
Posts: 4
You can use xargs

seq 1 14 | xargs -n 6
Reply With Quote
  #5 (permalink)  
Old 03-21-2007
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 758
Code:
#!/usr/bin/perl
my @num = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
for my $i (0 .. $#num) {
    printf ("%-8d", $num[$i]);
    print "\n" if (($i + 1) % 6 == 0);
}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0