Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array vs List output

6 views
Skip to first unread message

Neeraj

unread,
Jan 2, 2013, 5:35:25 AM1/2/13
to begi...@perl.org
Hi,

I am new to Perl and perplexed with output of printing entire Array vs
printing each element

+4 ## check Array vis-a-vis List
+5
+6 @arr = qw <hello happy new year 2013>;
+7 print "my array is : @arr \n";
+8
+9 ## lets print in a loop
+10 my $i = 0;
+11 while ($i <= $#arr)
+12 {
+13 print $arr[$i];
+14 $i += 1;
+15 };
+16 print "\n";


I expected the output of printing Array in line 7 to be same as printing
each element.
However, output of line 7 is : hello happy new year 2013
while printing each element: hellohappynewyear2013

Why do i have white-space between words when printing array while not
getting white-space when printing individual element.

Thanks,
Neeraj

David Precious

unread,
Jan 2, 2013, 5:54:40 AM1/2/13
to begi...@perl.org
On Wed, 2 Jan 2013 16:05:25 +0530
Neeraj <neeraj...@gmail.com> wrote:

> Why do i have white-space between words when printing array while not
> getting white-space when printing individual element.

perlfaq explains this - from perlfaq5:

Why do I get weird spaces when I print an array of lines?
(contributed by brian d foy)

If you are seeing spaces between the elements of your array when
you print the array, you are probably interpolating the array in
double quotes:

my @animals = qw(camel llama alpaca vicuna);
print "animals are: @animals\n";

It's the double quotes, not the "print", doing this. Whenever
you interpolate an array in a double quote context, Perl joins
the elements with spaces (or whatever is in $", which is a space
by default):

animals are: camel llama alpaca vicuna

This is different than printing the array without the
interpolation:

my @animals = qw(camel llama alpaca vicuna);
print "animals are: ", @animals, "\n";

Now the output doesn't have the spaces between the elements
because the elements of @animals simply become part of the list
to "print":

animals are: camelllamaalpacavicuna

You might notice this when each of the elements of @array end
with a newline. You expect to print one element per line, but
notice that every line after the first is indented:

this is a line
this is another line
this is the third line

That extra space comes from the interpolation of the array. If
you don't want to put anything between your array elements,
don't use the array in double quotes. You can send it to print
without them:

print @lines;



--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


*Shaji Kalidasan*

unread,
Jan 2, 2013, 6:15:36 AM1/2/13
to Neeraj, begi...@perl.org
Neeraj,

If you print an array inside double quotes, each item of the array is separated by the value specified in Perl special variable $" which is the Output list separator. (interpolated lists)

By default the value of $" is space

So you can rewrite your code modifying the Output list separator

#Example 1


## check Array vis-a-vis List

@arr = qw <hello happy new year 2013>;

{
local $" = "";


print "my array is : @arr \n";
}

## lets print in a loop
my $i = 0;
while ($i <= $#arr)
{
    print $arr[$i];
    $i += 1;
}
print "\n";

or place the array outside double quotes,in such case interpolation will not happen.

#Example 2


## check Array vis-a-vis List

@arr = qw <hello happy new year 2013>;

print "my array is : ", @arr, "\n";

## lets print in a loop

my $i = 0;
while ($i <= $#arr)
{
    print $arr[$i];
    $i += 1;
}
print "\n";
 
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
From: Neeraj <neeraj...@gmail.com>
To: begi...@perl.org
Sent: Wednesday, 2 January 2013 4:05 PM
Subject: Array vs List output

John W. Krahn

unread,
Jan 2, 2013, 7:41:58 AM1/2/13
to Perl Beginners
*Shaji Kalidasan* wrote:
> Neeraj,
>
> If you print an array inside double quotes, each item of the array is
> separated by the value specified in Perl special variable $" which is
> the Output list separator. (interpolated lists)

It is just the _List Separator_ , it has nothing to do with output.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

John W. Krahn

unread,
Jan 2, 2013, 7:41:55 AM1/2/13
to Perl Beginners
Neeraj wrote:
> Hi,
>
> I am new to Perl and perplexed with output of printing entire Array vs
> printing each element
>
> +4 ## check Array vis-a-vis List
> +5
> +6 @arr = qw<hello happy new year 2013>;
> +7 print "my array is : @arr \n";
> +8
> +9 ## lets print in a loop
> +10 my $i = 0;
> +11 while ($i<= $#arr)
> +12 {
> +13 print $arr[$i];
> +14 $i += 1;
> +15 };
> +16 print "\n";

In Perl that is usually written as:

## lets print in a loop
for my $i ( 0 .. $#arr )
{
print $arr[$i];
}

Or as:

## lets print in a loop
for my $i ( @arr )
{
print $i;
}

Or simply:

## lets print an array
print @arr;

*Shaji Kalidasan*

unread,
Jan 2, 2013, 8:00:26 AM1/2/13
to John W. Krahn, Perl Beginners
John,

Thanks for your clarification.

I used to call it as Output List Separator, now I knew it is just List Separator.


 
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
From: John W. Krahn <jwk...@shaw.ca>
To: Perl Beginners <begi...@perl.org>
Sent: Wednesday, 2 January 2013 6:11 PM
Subject: Re: Array vs List output

-- To unsubscribe, e-mail: beginners-...@perl.org
For additional commands, e-mail: beginne...@perl.org
http://learn.perl.org/

0 new messages