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

Hello! I have a statement in my code as follows:

print "\na:$wrds[11]\t b:$wrds[12]\t c:$wrds[13]\t d:$wrds[14]\t e:$wrds[15]";

It's corresponding output I saw was as follows:

         d:      e:0x4024d018    c:

Now, I want to understand where did a:, b: go? and why was the order d:, e: and c: instead of natural English alphabet order?

Replies are listed 'Best First'.
Re: understanding print
by FunkyMonk (Bishop) on Aug 11, 2008 at 14:05 UTC
    Does $words[13] contain chr 13 (carriage return)?

    Update:

    use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper $words[13];
    I think you'll see "\r" in there somewhere.

    Update^2: s/say/print/;

      Hello FunkyMonk! Thanks for replying. Incidently my code  wrds[13] is what had contained the information that I needed. The code was working with information from  wrds[13]. Unfortunately I could not try Data::Dump but the coincidence of you suspecting something about  wrds[13] and it containing my info. is strange. I used a work around (of manually putting the string in my variable which is not what I had written the code for) and went ahead with my work. But I shall be thankful if order change is understood. Regards.

        What FunkyMonk was getting to is that printing a carriage return ("\r") causes the cursor to move to the first column on many terminals. We suspect the value of $wrds[13] contains that character.

        >perl -le"print qq{abcdef\rghi}" ghidef

        Unfortunately I could not try Data::Dump

        Why not?

Re: understanding print
by Anonymous Monk on Aug 11, 2008 at 14:35 UTC
    Terminals are tricky
    C:\> hexdump 2echo >2 C:\> hexdump 2 00000000: 45 43 48 4F 20 69 73 20 - 6F 6E 2E 0D 0A |ECHO is o +n. | 0000000d; C:\> od 2 0000000 041505 047510 064440 020163 067157 006456 000012 0000015 C:\> od -tc 2 0000000 E C H O i s o n . \r \n 0000015 C:\> perl -e"print qq~$_\r~ for 1 .. 3" 3 C:\> perl -e"print qq~$_\r~ for 1 .. 3" >2 C:\> hexdump 2 00000000: 31 0D 32 0D 33 0D - |1 2 3 | 00000006; C:\> od 2 0000000 006461 006462 006463 0000006 C:\> od -tc 2 0000000 1 \r 2 \r 3 \r 0000006 C:\> od -ta 2 0000000 1 cr 2 cr 3 cr 0000006 C:\>od -abcx 2 0000000 1 cr 2 cr 3 cr 061 015 062 015 063 015 1 \r 2 \r 3 \r 0d31 0d32 0d33 0000006
Re: understanding print
by graff (Chancellor) on Sep 20, 2008 at 17:42 UTC
    I'm sure you have worked out at least one good solution by now, but FWIW, you could try doing this first, before the print statement:
    for ( @wrds[11..15] ) { s/^\s+//; s/\s+$//; s/\s+/ /g }
    That's one of those good habits to get into when handling unreliable input data and trying to format it for nice, coherent printing.