Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Format not adding to line when ending in spaces

by spudnic (Acolyte)
on Dec 13, 2003 at 23:33 UTC ( #314575=perlquestion: print w/replies, xml ) Need Help??

spudnic has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I'm having a problem using formats. The issue arises when the variable that is to be printed last in a line contains only spaces nothing is added to the end of the line. This variable on most rows will be something other than all spaces or it wouldn't be a problem. I must have these spaces as they are required for the parser to find something in those columns.

Example:

#!/usr/bin/perl # This script is called format_test format = @<<<<@>>> $field1,$field2 . $field1 = "Hello"; $field2 = " "; write;
When you run
./format_test | wc -m
you get 6.

The parser freaks out if there is anything after where I need the spaces, so I can't force padding by sticking something in the next field.

Any suggestions? The parser is an old mainframe job, so getting it modified would be a big pain.

Thanks

Replies are listed 'Best First'.
Re: Format not adding to line when ending in spaces
by Abigail-II (Bishop) on Dec 14, 2003 at 00:23 UTC
    I think you've stumbled on an undocumented feature of formats, that trailing spaces are removed. (No, I don't mean a bug).

    Since you really do need the space, I suggest using either (s)printf, or to postprocess your output (which can be done from the same program using a pipe open - see 'man perlopentut') and to add the trailing spaces yourself.

    It'll depend on the complexity of your format which is the easiest way to go.

    Abigail

Re: Format not adding to line when ending in spaces
by pg (Canon) on Dec 14, 2003 at 03:29 UTC

    This is not a bug, and even cannot be called as undocumented feature. format is meant to handle report format, and in this case, the output did strictly follow the format you specified. Visually you got what you asked. It is the wrong expectation to have format work as an alternative version of sprintf.

    It is not really that scary to make it work for you. You can just do something like:

    use strict; use warnings; my ($field1, $field2); format = @<<<<@>>>@ $field1,$field2, 'x'; . $field1 = "Hello"; $field2 = " "; write; $field1 = "ab"; $field2 = "cd"; write;

    The other side is mainframe right? On mainframe there is a tool to chop away certain columns of a fixed-length file (super easy). Just ask your mainframe guys to add one step in their JCL, before the step they use your data.

    If your mainframe program is written in COBOL, simply change the copy file, which contains the definition of the file they expect from you. If the program is well designed, all you need to do it to change one copy book and recompile whole bunch of programs use that copy file. (This is not recommended, what I said in last paragraph is much better and much less risk.)

    Mainframe is not scary at all.

Re: Format not adding to line when ending in spaces
by melora (Scribe) on Dec 14, 2003 at 02:37 UTC
    Personally, I'm somewhat inclined to think that's a bug. Look at it this way: Why are space characters treated like second-class data? Just because you're a 20 and not a 30... ;) How about taking just that last field out of the format, and using a print (or printf) to print the last field on the line? You could leave the rest of them undisturbed, so it would be a small code change. Melora
Re: Format not adding to line when ending in spaces
by Sandy (Curate) on Dec 15, 2003 at 00:50 UTC
    Hi,

    I'm not a whiz at pack and unpack, so I'm not sure how to right justify and left justify yet (I left the camel book at work), but at least it handles spaces (the capital 'A' means ascii (or text?) , and pad with spaces).

    Start with this, and see how it goes...

    $template = "A5 A4"; $field1 = "Hello"; $field2 = " "; $str = pack ($template,$field1,$field); print "my string is <$str>\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314575]
Approved by Roger
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-11-29 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?