Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Pack decimal possibility

by mikeward61 (Initiate)
on May 26, 2010 at 21:10 UTC ( #841825=perlquestion: print w/replies, xml ) Need Help??

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

We create files on our Linux servers that are NDM'd and processed on our mainframes. We need the ability to create a trailer record that contains a packed field indicating the number of records that should be in the file (i.e. 240 records would be x'0000240C'). Is this possible in Perl?

Replies are listed 'Best First'.
Re: Pack decimal possibility
by ReturnOfThelonious (Beadle) on May 26, 2010 at 21:35 UTC
    Specifically,
    my $packed = pack "H*", sprintf "%07dC", $num_records;
    would give you a PIC S9(7) COMP-3 packed decimal (positive only).

    Perl can count the records and add the trailer record, too, but I don't know what record format you're using.

      Thanks, this worked perfectly...
Re: Pack decimal possibility
by BrowserUk (Patriarch) on May 26, 2010 at 21:35 UTC

    pack doesn't support this directly. This will do the job, though it can probably be made significantly more efficient:

    sub toComp3{ my $num = shift; my @digits = split'',abs $num; unshift @digits, 0 unless @digits & 1; push @digits, $num < 0 ? 13: 12; my $comp3 = ''; $comp3 .= chr( shift( @digits ) * 16 + shift( @dig +its ) ) while @digits; return $comp3 } print unpack 'H*', toComp3( 240 );; 240c

    It also doesn't cover unsigned numbers. Though a flag to indicate signed-ness could be added.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Pack decimal possibility
by chuckbutler (Monsignor) on May 26, 2010 at 23:37 UTC

    There is a module, Convert::IBM390, that supports various conversion functions for data to/from a mainframe. One is packeb:

    use Convert::IBM390 qw(packeb); my $comp3 = packeb( 'p4', 240 ); #77 A PIC S9(7) COMP-3. print $comp3; __END__ ~~output~~ 0x0000240c (of course, in binary)

    It works well, and handles negative values.

    Good luck. -c

Re: Pack decimal possibility
by Corion (Patriarch) on May 26, 2010 at 21:13 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-10-03 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (15 votes). Check out past polls.

    Notices?