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

Hi Monks, Sorry to tax your patience with a simple question like this, but this is driving me crazy:
use strict; my @days; @days = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); print @days, "\n"; print qw(Monday Tuesday Wednesday Thursday Friday), "\n";
When I use qw and print out these lists why are the results all run together with no spaces--MondayTuesdayWednesday, etc. I thought useing qw would allow Perl to print my array elements or list with the spaces as entered above. Can you help me understand why the results are run together? I know this must rank as one of the dumbest questions ever, but I'd appreciate the advice nonetheless.

Replies are listed 'Best First'.
Re: Using qw
by GrandFather (Saint) on Nov 18, 2005 at 01:52 UTC

    In both cases you have an array and print does not insert any extra characters between the elements of an array. You can use join to do that however:

    use strict; my @days; @days = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); print ((join ' ', @days), "\n"); print join ' ', qw(Monday Tuesday Wednesday Thursday Friday), "\n";

    Prints:

    Monday Tuesday Wednesday Thursday Friday Saturday Sunday Monday Tuesday Wednesday Thursday Friday

    To see why I put the brackets in try these two lines:

    print ((join ' ', @days), "x\n"); print join ' ', @days, "x\n"; #prints: Monday Tuesday Wednesday Thursday Friday Saturday Sundayx Monday Tuesday Wednesday Thursday Friday Saturday Sunday x

    Note the space before the x in the second line because @days, "x\n" is a list that join acts on, rather than the paraneter for join followed by a parameter for print.


    DWIM is Perl's answer to Gödel
      Excellent explanation GrandFather. Thank you and thank you to everyone else who wrote in. I am working through your examples.
Re: Using qw
by BUU (Prior) on Nov 18, 2005 at 01:47 UTC
    No, qw simply creates a list from the string you enter, SPLIT ON WHITE SPACE. There for your resulting list contains no white space, so print has none to output.

    You may be confused by the behaviour of print "@days", which happens because any array in string context turns in to a list of it's elements joined by $", so "@days" is equivalent to join $",@dats.

      OP didn't actually use "@days". However to illustrate the difference:

      use strict; my @days; @days = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); local $" = ', '; print "@days\n"; print @days, "\n"

      Prints:

      Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday MondayTuesdayWednesdayThursdayFridaySaturdaySunday

      DWIM is Perl's answer to Gödel
Re: Using qw
by vishi83 (Pilgrim) on Nov 18, 2005 at 05:37 UTC

    Hi,

    I hav jus made few changes to your code ..

    use strict; use warnings; my @days; @days = qw/Monday Tuesday Wednesday Thursday Friday Saturday Sunday/; print "@days","\n";
    This will print as you wanted ...

    Thank you

    A perl Script without 'strict' is like a House without Roof; Both are not Safe;