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.seh...@gmail.com>
To: beginners@perl.org 
Sent: Wednesday, 2 January 2013 4:05 PM
Subject: Array vs List output
 
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

Reply via email to