Sponsored Content
Top Forums Programming [Perl] Different printf formating for different print options Post 302776635 by durden_tyler on Wednesday 6th of March 2013 03:11:44 PM
Old 03-06-2013
Code:
$
$
$ cat -n example_2.pl
     1  #!/usr/bin/perl -w
     2  use strict;
     3  my $option = $ARGV[0];
     4  my %x = (
     5            0,
     6              [
     7                [ qw (%s %s %s %s %s) ],                      # format array ref
     8                [ qw (Value1 Value2 Value3 Value4 Value5) ],  # header array ref
     9                [ qw (one two three four five) ]              # data array ref
    10              ],
    11            1,
    12              [
    13                [ qw (%s %s %s) ],
    14                [ qw (Value1 Value2 Value3) ],
    15                [ qw (one two three) ]
    16              ],
    17            2,
    18              [
    19                [ qw (%s %s %s) ],
    20                [ qw (Value1 Value4 Value5) ],
    21                [ qw (one four five) ]
    22              ]
    23          );
    24  $option = 0 if (! defined $option) or (! defined $x{$option});
    25
    26  my $fmt  = join(",", @{$x{$option}->[0]})."\n";
    27  my @hdr  = @{$x{$option}->[1]};
    28  my @data = @{$x{$option}->[2]};
    29
    30  printf ($fmt, @hdr);
    31  printf ($fmt, @data);
$
$ perl example_2.pl 1
Value1,Value2,Value3
one,two,three
$
$ perl example_2.pl 2
Value1,Value4,Value5
one,four,five
$
$ perl example_2.pl 3
Value1,Value2,Value3,Value4,Value5
one,two,three,four,five
$
$ perl example_2.pl 4
Value1,Value2,Value3,Value4,Value5
one,two,three,four,five
$
$ perl example_2.pl 0
Value1,Value2,Value3,Value4,Value5
one,two,three,four,five
$
$ perl example_2.pl
Value1,Value2,Value3,Value4,Value5
one,two,three,four,five
$
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 ... (4 Replies)
Discussion started by: seismic_willy
4 Replies

2. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies

3. Shell Programming and Scripting

Date Formating in Perl

Hi All, Can anybody tell me why is there a "0" in my output of $date_today ? #!/usr/local/bin/perl $date_today = system "date '+%y%m%d'"; print "$date_today\n"; Output: $ perl test4 080908 0 (3 Replies)
Discussion started by: Raynon
3 Replies

4. Shell Programming and Scripting

AWK: formating number without printf

Hello, I wrote a script that does lot of things, and I would like to change the format of a number but without printing it now (so I don't want to use printf as it will print the value immediately). Schematically here is what I have: awk 'BEGIN{number=0.01234567} $1==$2{$3=number}... (5 Replies)
Discussion started by: jolecanard
5 Replies

5. Shell Programming and Scripting

file formating in Perl

Hi, I am new to unix , I have a requirement for formating the input file and generate the output file as per the downstream requirement .. My application receiving a text input file having 4 field and my application need to check each field and if some value of a field is blank ..then it need... (1 Reply)
Discussion started by: julirani
1 Replies

6. Shell Programming and Scripting

How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf. If I try printing it using printf it is printing as follows. output ------- str1 str2 str3 str4 btw I'm working in AIX. This is my first post in this forum :) regards, rakesh (4 Replies)
Discussion started by: enigmatrix
4 Replies

7. UNIX for Advanced & Expert Users

remove print formating from printer output file

I have a print file taken from the print spooler and I want to delete all the formatting leaving only the text. If you vi the file it shows "\304\304 ...." which translates into a printed line on print output. I need to be able to edit and pass this file to another process Thnaks (10 Replies)
Discussion started by: petercp
10 Replies

8. Shell Programming and Scripting

Formating of query variable in perl

Hi , I am facing error in perl when I assign a below query in a varibale $query because of new line charchters $query= SELECT XYZ , ABC , c2 , c3 , c4 FROM t1 how can i get rid of new line charchters with out changing the... (2 Replies)
Discussion started by: gvk25
2 Replies

9. Shell Programming and Scripting

What's the difference between print and printf in command?

For example, in this command: ls /etc/rc0.d/ -print ls /etc/rc0.d/ -printfThe outputs are quite different, why? (7 Replies)
Discussion started by: Henryyy
7 Replies

10. Shell Programming and Scripting

How to combine print and printf on awk

# cat t.txt 2,3,4,5,A,2012-01-01 00:00:28 2,6,4,5,A,2012-01-02 00:00:28 2,7,4,5,A,2012-01-02 02:00:28 # awk -F"," '{OFS=",";print $2,"";printf("%s", strftime("%m%d%y",$6));printf("%s", strftime("%H%M%S \n",$6));print ("",$1)}' t.txt 3, 010170073332 ,2 6, 010170073332 ,2 7,... (3 Replies)
Discussion started by: before4
3 Replies
MooseX::Storage::Util(3pm)				User Contributed Perl Documentation				MooseX::Storage::Util(3pm)

NAME
MooseX::Storage::Util - A MooseX::Storage swiss-army chainsaw DESCRIPTION
This module provides a set of tools, some sharp and focused, others more blunt and crude. But no matter what, they are useful bits to have around when dealing with MooseX::Storage code. METHODS
All the methods in this package are class methods and should be called appropriately. peek ($data, %options) This method will help you to verify that the serialized class you have gotten is what you expect it to be before you actually unfreeze/unpack it. The $data can be either a perl HASH ref or some kind of serialized data (JSON, YAML, etc.). The %options are as follows: format If this is left blank, we assume that $data is a plain perl HASH ref otherwise we attempt to inflate $data based on the value of this option. Currently only JSON and YAML are supported here. key The default is to try and extract the class name, but if you want to check another key in the data, you can set this option. It will return the value found in the key for you. Introspection meta TODO
Add more stuff to this module :) BUGS
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. AUTHOR
Stevan Little <stevan.little@iinteractive.com> COPYRIGHT AND LICENSE
Copyright 2007-2008 by Infinity Interactive, Inc. <http://www.iinteractive.com> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-02-28 MooseX::Storage::Util(3pm)
All times are GMT -4. The time now is 02:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy