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

Hi,
in the following block of code (taken from a program that I wrote), I get the error:
Use of uninitialized value $" in join or string at my_implementation.p +l line 215.

Can you tell me what I must add/correct?
open OUT_A, ">$a_file"; for my $d ( 0 .. $#table_A ) { my $row1 = "@{$table_A[$d]}"; $row1 =~s/\s/\t/g; print "$row1\n"; } close OUT_A ;
.
The error line is: my $row1 = "@{$table_A[$d]}";

Replies are listed 'Best First'.
Re: Why do I get this error?
by LanX (Saint) on May 12, 2014 at 00:12 UTC
    edit

    $" is a special variable defining how array elements are separated within a string interpolation. Its set by default, if its missing now then YOU must have set it to undef before. See $LIST_SEPARATOR in perlvar

    some more thoughts

    Do you use strict and warnings?

    Why do you iterate so complicated instead of using a foreach $elem (@array) ... etc ?

    Why a string interpolation instead of join "\t", @$elem ?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      But I have a Array of Arrays there...
      How can I use join?
      Also, if you use foreach, doesn't it mean that you lose the order of elements? Or not?
        > But I have a Array of Arrays there...

        $elem is an array ref, you can also call it $a_ref or whatever explains best the content ($a_row for instance)

        > How can I use join?

        like shown, did you notice the @ in front of @$elem, it dereferences the array ref.

        > Also, if you use foreach, doesn't it mean that you lose the order of elements?

        Nope! Try it out...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        And regarding the $", I didn't do anything, it's actually first time I come across this thing...I don't know what I am supposed to do to be honest!
Re: Why do I get this error?
by GrandFather (Saint) on May 12, 2014 at 01:36 UTC

    LanX is right about $" being a standard variable and global. He is also right that its default value must have been changed somewhere (possibly in a module you use) to undef to raise the warning you are seeing.

    You can "fix" the value of $" by:

    local $" = ' '; open ...

    which restores the default space used for $".

    If you recently added use warnings to your script that would show a warning for a nasty condition that you previously weren't aware of. An array interpolated into as string behaves like:

    my $arrayStr = join $", @{$table_A[$d]}; my $row1 = "$arrayStr";

    so the undef value in $" would have turned into an empty string and the array elements would have been stuck together instead of separated by single spaces.

    Perl is the programming world's equivalent of English
      > You can "fix" the value of $" by:

      > local $" = ' ';

      Well I didn't suggest this, cause any row element containing spaces will contain tabs after the s/\s/\t/g.

      Using an explicit join is much cleaner.

      Otherwise better local $" = "\t"; and no substitution at all...

      Cheers Rolf

      ( addicted to the Perl Programming Language)