Printing arrays

Discussion in 'Perl' started by juliadream, Jan 19, 2005.

  1. juliadream

    juliadream Guest

    Can anyone tell my why

    print @array

    produces output different from

    print "@array"


    The second one embeds spaces between the items; the first one does not.
    What is the rationale behind this?

    Example:

    push @temparray, "hello", "again", "today";

    print @temparray; # Output: helloagaintoday

    print "@temparray"; # Output: hello again today
    juliadream, Jan 19, 2005
    #1
    1. Advertising

  2. juliadream wrote:
    > Can anyone tell my why
    >
    > print @array
    >
    > produces output different from
    >
    > print "@array"
    >
    >
    > The second one embeds spaces between the items; the first one does
    > not. What is the rationale behind this?


    You are slightly mistaken.

    The first example doesn't print an array, it prints a list. And print()
    prints lists as a direct succession of their elements.

    In the second example print() prints just a single scalar, which happens to
    be a string.
    And this string was created by interpolating an array inside of double
    quotes which is defined as inserting the value of $" between the elements.

    jue
    Jürgen Exner, Jan 20, 2005
    #2
    1. Advertising

  3. juliadream wrote:
    > Can anyone tell my why
    >
    > print @array
    >
    > produces output different from
    >
    > print "@array"
    >
    >
    > The second one embeds spaces between the items; the first one does not.
    > What is the rationale behind this?
    >
    > Example:
    >
    > push @temparray, "hello", "again", "today";
    >
    > print @temparray; # Output: helloagaintoday
    >
    > print "@temparray"; # Output: hello again today
    >
    >
    >
    >
    >



    print @array

    is using @array in a list context.

    It is therefore the same as saying

    print $array[0], $array[1], ...

    And when you ask print to print a list of things like that, it separates
    them with whatever is in the Output Field Separator, which is called
    $, (which is the same as $OUTPUT_FIELD_SEPARATOR). This defaults to null.

    On the other hand,

    print "@array"

    is just asking print to print one thing: a string. Not a list of things to print.

    The string has been created by expanding the array into the string.

    This follows the rules for expanding arrays into strings, which are that
    the array elements will be separated in the string by whatever is
    in $" (which is the same as $LIST_SEPARATOR). This defaults to a space.

    You can read about these variables in man perlvar. You could change
    either of them in your code to force

    print @array

    and

    print "@array"

    to give you the same thing.


    Dream that dream.

    Martin.
    Martin Gregory, Jan 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,484
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    503
    msalters
    Dec 9, 2004
  3. Philipp
    Replies:
    21
    Views:
    1,111
    Philipp
    Jan 20, 2009
  4. iffy agbim
    Replies:
    1
    Views:
    98
    Mark Andrews
    May 24, 2004
  5. iffy agbim
    Replies:
    0
    Views:
    97
    iffy agbim
    May 21, 2004
Loading...

Share This Page