Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

printf not behaving as expected

by bobdabuilda (Beadle)
on May 04, 2012 at 00:07 UTC ( #968836=perlquestion: print w/replies, xml ) Need Help??
bobdabuilda has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys. Just playing with another project to try and both keep my hand in the Perl world, as well as try and learn some new tricks, etc.

The below code is a small snippet of code which is called from a couple of different modules. However, it's not behaving the way I want or expect it to. Any chance you can point out where I'm going wrong please?

while (<DATA>) { chomp; (@fields) = split(/\|/); $cat_key = $fields[0]; $call_seq = $fields[1]; $copy_num = $fields[2]; $item_resctl = $fields[3]; $item_id = $fields[4]; $num_copies = $fields[5]; $call_holds = $fields[6]; $num_reserve_controls = $fields[7]; $bound_level = $fields[8]; $callnum = $fields[9]; $num_callnums = $fields[10]; $review = $fields[11]; $cat_acnt = $fields[12]; $status = $fields[13]; $copies_on_order = $fields[14]; $title_holds = $fields[15]; $title = $fields[16]; $discard = "YES"; # Added for debugging purposes print("Item ID: " . $item_id . "\n"); # Trimmed additional checks and balances for brevity if ($discard eq "YES") { # This is the problem child printf("%d|%d|%d|%d|%d|%d|%s|\n",$cat_key,$call_seq,$item_id,$copy +_num,$num_callnums,$num_copies,$title); } else { # This line works fine print STDERR "$cat_key|$call_seq|$item_id|$copy_num|\n"; } } __DATA__ 111|65|1|0|34211018032711 |1|0|0|NONE|CD ROC SYS|4|0|0|1|0|0|CD1| 611|38|1|0|34211012797211 |1|0|0|NONE|ANF 283.092 SPO|1|0|0|1|0|1|BOO +K1| 1011|98|3|0|34211018090411 |4|0|0|NONE|P EDW|11|0|0|1|0|0|BOOK2| 1011|4|2|0|34211011193311 |1|0|0|NONE|ANF 070.43 WIL|1|0|0|1|0|0|BOOK +3| 1211|12|1|0|34211014782811 |1|0|0|NONE|ANF 615.3257 MUR|1|0|0|1|0|0|B +OOK4| 1212|87|1|0|34211017402911 |1|0|0|NONE|J BLY|6|0|0|1|0|0|BOOK5| 1711|82|1|0|34211016966711 |1|0|0|NONE|P WIL|3|0|0|1|0|0|BOOK6| 2111|5|1|0|34211002936811 |1|0|0|NONE|JNF 981.1 GHE|1|0|0|1|0|1|BOOK7 +| 2411|27|1|0|34211015588811 |1|0|0|NONE|JNF 597 HAR|2|0|0|1|0|0|BOOK8| 2711|34|1|0|34211016741111 |1|0|0|NONE|AF SAY|2|0|0|1|0|0|BOOK9|

This results in the output of :

Item ID: 34211018032711 111|65|-1|1|4|1|CD1| Item ID: 34211012797211 611|38|-1|1|1|1|BOOK1| Item ID: 34211018090411 1011|98|-1|3|11|4|BOOK2| Item ID: 34211011193311 1011|4|-1|2|1|1|BOOK3| Item ID: 34211014782811 1211|12|-1|1|1|1|BOOK4| Item ID: 34211017402911 1212|87|-1|1|6|1|BOOK5| Item ID: 34211016966711 1711|82|-1|1|3|1|BOOK6| Item ID: 34211002936811 2111|5|-1|1|1|1|BOOK7| Item ID: 34211015588811 2411|27|-1|1|2|1|BOOK8| Item ID: 34211016741111 2711|34|-1|1|2|1|BOOK9|

So - why is it that $item_id appears to be being eaten by the printf line?? What have I done wrong? :)

EDIT: Hmmm... have done some more research on printf... thinking the issue could be that the number is too large to be interpreted as an Integer? If that's the case, any suggestions on how better to deal with it? Perhaps a string?

Replies are listed 'Best First'.
Re: printf not behaving as expected
by chromatic (Archbishop) on May 04, 2012 at 00:50 UTC

    Are you using a 32-bit Perl, or a Perl with 32-bit integers? If so, what happens if you use the pattern %ld for that field? (You can always interpolate the variable directly instead of using printf, of course.)

    Improve your skills with Modern Perl: the free book.

Re: printf not behaving as expected
by eyepopslikeamosquito (Chancellor) on May 04, 2012 at 02:30 UTC
    Running this test program:
    my $v = 123456789012345; printf "%d %ld %u %15.0f %s\n", $v, $v, $v, $v, $v;
    on my 32-bit perl 5.8 produces:
    -1 -1 4294967295 123456789012345 123456789012345
    So it looks like %s and %15.0f are worth a shot.

    Later versions of Perl on certain platforms might further support %ll and %q (C "long long" et al type, typically 64-bit integers) according to sprintf.

Re: printf not behaving as expected
by johngg (Abbot) on May 04, 2012 at 09:18 UTC

    As an aside, you could save yourself a lot of typing by assigning the split directly to your scalar variables.

    my( $cat_key, $call_seq, $copy_num, $item_resctl, $item_id, $num_copies, $call_holds, $num_reserve_controls, $bound_level, $callnum, $num_callnums, $review, $cat_acnt, $status, $copies_on_order, $title_holds, $title, ) = split /\|/;

    I hope this is useful.



Re: printf not behaving as expected
by BrowserUk (Pope) on May 04, 2012 at 01:22 UTC

    Try %u instead of %d

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    The start of some sanity?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://968836]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-02-21 16:41 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (314 votes). Check out past polls.