![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|
|||
|
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);
}
|
|||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|