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

I get mangled output with first printf but the second printf below works. Notice that I put the phone number last in the second one? Based on tests, if I print anything after the phone number it results in corruption.
printf "Dialing %s of %s on %s: %s, %s, %s\n", $index, $total, $ref->{'datetime'}, $ref->{'host'}, $ref->{'pho +ne'}, $ref->{'description'}; printf "Dialing %s of %s on %s: %s, %s, %s\n", $index, $total, $ref->{'datetime'}, $ref->{'description'}, $ref +->{'host'}, $ref->{'phone'};
Output (mangled first) looks like:
, UK Routersf 16 on Fri Jan  4 18:44:40 2002: UK9Z, 011441234567890
Dialing 01 of 16 on Fri Jan  4 18:41:03 2002: UK Routers, UK9Z, 011441234567890
I even tried print "RJL:/$ref->{'phone'}\n"; with similar mangled results.

The phone number above was altered, but the real number does start with 01144. I also printed the phone number using %#vx and did not see any "hidden" special characters.

I am using the perl listed below on an Ultra-10 running Solaris 8 with Maintenance Update 6.

This is perl, v5.6.1 built for sun4-solaris-thread-multi
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 630 provided by ActiveState Tool Corp. http://www.ActiveState.com
Built 23:31:40 Oct 31 2001
Any ideas what is happening? If I was going to guess the phone number is being treated as an Octal number. If so, why would it in this context (or how can I escape it)? And why does it still print correctly (shouldn't I see 44... instead)? -- Argel

Edit kudra, 2002-01-10 Replaced some pre with code

Replies are listed 'Best First'.
(Ovid) Re: Mangled print output -- any ideas why?
by Ovid (Cardinal) on Jan 05, 2002 at 06:33 UTC

    The phone number is not being treated as an octal number. Numbers are only considered octal if Perl encounters a constant number in the source with a leading zero.

    my $oct = 017; my $dec = "017"; print $oct+0, $/, $dec+0; #plus zero forces it to be numeric

    That prints 15, newline, 17. However, as for your actual problem, I don't see what's going on. Have you tried printing directly?

    print "Dialing $index of $total on $ref->{datetime}: $ref->{host}, $re +f->{phone}, $ref->{description}\n";

    Hmm... I see that that you did try that. Never mind.

    I seem to recall a similar problem on Windows when accepting reading data from STDIN. Had something to do with the cntl-Z that you had to use to end input eating up the next line that's printed. I had to insert an extra print before the actual printing to get around that. Have you tried that?

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Mangled print output -- any ideas why?
by dvergin (Monsignor) on Jan 05, 2002 at 13:46 UTC
    It would appear that you have a carriage return embedded in the end of the string in $ref->{'phone'}.

    In the first line of output the program is printing what you expect to the screen up to the end of the phone number. Then it is coming back to the beginning of the same line and printing ", UK Routers" over the top of "Dialing 01 o".

    Best to go back and double check the parsing-in of the data (Did you chop a CRLF instead of chomp? Did you import the data from a Mac?) -- or failing that, fix it here.     $ref->{'phone'} =~ s/\015$//; You might be tempted to do:     $ref->{'phone'} =~ s/\r$//; And it may likely work in this case. But be aware that \r varies from system to system.

      Right you are dvergin! There is indeed a CR character at the end.
      I think I get what the problem is. My datafile came from a PC and thus has a CRLF in it. But since I am now running this on UNIX (migrating the app from Windows NT/2K to UNIX) chomp only gets rid of the LF and leaves the CR?
      Guess the moral of the story is that straight substitutions are better than using chomp or chop!
      Thanks for the help everyone!
        The problem you are seeing with chomp is that Perl uses the line terminator appropriate to the current platform, so if you're in UNIX, Perl chomps just the LF, which is the UNIX value of \n. If you had used the chomp in Win32 you would have removed CRLF, as that is the \n value for that platform. I presume (and welcome corrections) that if your phone data had come from a Mac, the UNIX chomp would not have removed anything, as for UNIX the lines would not appear to have terminators at all (and so the data would have been one long line). This is nicely described in perlport.

        As to fixing it, while chomp appears to be a logical choice, it is an approach of "remove the bad" when you believe you know what the bad is, but as this case has shown you often can't be certain of what the bad is. It is probably safer to reverse this to an approach of "keep the good", with something like tr/0-9//cd, especially since your phone number field is so easy to specify.

        --
        I'd like to be able to assign to an luser

Re: Mangled print output -- any ideas why?
by Anonymous Monk on Jan 05, 2002 at 11:21 UTC
    What did you see when you printed using %#vx ?