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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Oct 31, 2004, 11:16 AM

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

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


my @array;

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

print @array;

displays 0123 while


my @array;

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

print "@array";

displays 0 1 2 3


Oct 31, 2004, 11:30 AM

Post #2 of 3 (772 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.

Thaumaturge / Moderator

Nov 1, 2004, 10:53 AM

Post #3 of 3 (767 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
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