CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
why the difference?

 



Larkdog
User

Oct 31, 2004, 11:16 AM

Post #1 of 3 (362 views)
why the difference? Can't Post

i don't understand why there is a difference in the following:


Code
   

my @array;

@array = (0,1,2,3);

print @array;



displays 0123 while


Code
  

my @array;

@array = (0,1,2,3);

print "@array";



displays 0 1 2 3

www.MrLarkins.com


KevinR
Veteran


Oct 31, 2004, 11:30 AM

Post #2 of 3 (360 views)
Re: [Larkdog] why the difference? [In reply to] Can't Post

when you use double quotes to print an array, Perl adds a space between the elements is all. I assume its to make the array more readable in a visual medium like a browser/monitor.

You may find this resource of value. Some of the FAQs are quite a bit beyond beginner level, but FAQ4 has some good information about using data in strings and array and hashes and so on.

http://www.perldoc.com/perl5.8.4/pod/perlfaq.html
-------------------------------------------------


davorg
Thaumaturge / Moderator

Nov 1, 2004, 10:53 AM

Post #3 of 3 (355 views)
Re: [Larkdog] why the difference? [In reply to] Can't Post

Think about what is actually passed to "print" in each case.

In the first case "print" gets three arguments. When "print" gets multiple arguments then it prints them separated by the contents of the variable $,. The default value for $, is an empty string, but you can change that.

In the second case "print" gets one argument which is the results of interpolating an array in a double quoted string. When an array is interpolated in a double quoted string, the individual elements are separated by the contents of the $" variable. The default value of $" is a single space but, once again, you can change it.

I cover this in more detail in this article.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives