[Perl] Different printf formating for different print options
Hi,
Struggling with single quotes, double quotes, etc.
I want to print a header line, followed by lines with actual values, based on a print option.
In real life it is going to be something like 15 print options and 50 values.
Output will be 1 header and several value lines.
In this example I have just 3 print options and 5 values.
Output is just 1 header and one value line.
For some reason I cannot get it working.
I get uninitialized values and quotes where I do not want them.
It expects too many values (seen by the extra comma's in the output).
And therefore I could use some expert help.
Here the example.pl code to make it, hopefully, more clear.
Result:
---------- Post updated at 02:17 PM ---------- Previous update was at 10:08 AM ----------
When I use:
I get the right output:
With:
I get:
Some debugging info:
The 'problem' is that $PrintHeader is seen as 1 string for the printf command.
That is why I get 2 uninitialized values.
---------- Post updated at 03:19 PM ---------- Previous update was at 02:17 PM ----------
This works.
Perhaps not very prety, but it does the job.
Had to use arrays.
And using a string is a bit more easy than using a variable, when working with arrays.
But I am just an amateur.
Always open for suggestion on how to make it more simple.
Build the line with one line one element printf statements, not a long template and a long argument list. It will be more readable, more maintainable, easy to comment and likely runs faster. After all, output is a stream, so throw the pieces into the stream one at a time. Here is a fragment of JAVA I wrote recently:
Last edited by DGPickett; 03-06-2013 at 04:18 PM..
Thanks for taking time to reply and for your suggestion.
Could you try to replace the data by variables (my $Value1 = "one", etc) and then try again ?
I get this:
The only way to make it work was using arrays for the values iso strings.
See the last part of the first post (I am having difficulties in replying to my own post in a separate post).
---------- Post updated at 03:23 PM ---------- Previous update was at 03:19 PM ----------
Quote:
Originally Posted by DGPickett
Here is a fragment of JAVA I wrote recently:
Would have to convert this idea to Perl and then have it shaped to my problem.
I find a solution already (last part of the first post) and that works for me.
I'm just saying that for good code structure, especially in printf situaitons where a template bit matches a data argument, doing one at a time give you a virtual array of lines where the template is right by the data and any comment, one field a line, give or take the occasional adjacent series of constants.
At a higher level, put all or minority record types in separate subroutines, so the flow is simple to follow: writeHeader, while ... writeData, writeTrailer.
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)
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)
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)
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)
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)
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)
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)
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)