in reply to Uninitialized Value Error When Interleaving

First, that's a really, really ugly place for the my declaration. It's carrying things just a bit too far. Move it somewhere else.

Secondly, running your sample produces no warnings for me. It probably shouldn't. However if @foo and @bar don't have the same number of elements (specifically, if @foo is short) then you'll get enough undef values in the array to make up the difference.

Thirdly, did you really mean to print and do the assignment at the same time? Yeah, you'll get the return value from the assignment printed, but is that really what you wanted? (Tends to be ugly, that's all).

  • Comment on Re: Uninitialized Value Error When Interleaving

Replies are listed 'Best First'.
Re: Re: Uninitialized Value Error When Interleaving
by La12 (Sexton) on Jul 03, 2001 at 03:07 UTC
    1) I will move the my declaration.
    2) Yes @foo is shorter. My code should have read...
    #!/usr/bin/perl use warnings; use strict; my @foo = ( 1 , 3 , 5 , 7 ); my @bar = (0, 2 , 4 , 6 , 8 ); print STDOUT my @foobar = map { $_, shift @foo } @bar;
    I'm assuming that this is my problem, no? I do get the proper result "012345678"...just with a warning.
    3) I guess I have a talent for ugly... and warnings :-(
      As for #2...no you don't get the proper results. What you're actually getting is: 012345678undef but of course undef doesn't print. :)

      Watch this. It's your code with two small modifications:

      use warnings; use strict; my @foo = ( 1 , 3 , 5 , 7 ); my @bar = (0, 2 , 4 , 6 , 8 ); my @foobar= map { $_, shift @foo } @bar; print STDOUT join(',', @foobar);
      When you run this, notice that the list is going to have a trailing ",". That's because join() knows there's an element after the 8 (undef!) and makes a slot for it. And notice the error changed, its now an uninit value in join.
        Thank you for the great explanation. I understand the problem.