On 6/8/11 Wed Jun 8, 2011 10:08 AM, "sono...@fannullone.us" <sono...@fannullone.us> scribbled:
> I ran across something interesting today. I'm getting different results for > an array depending on how it's used, as follows: > > > say @fieldnames; > prints > UIDGroupNamelastmodcategoryhtmlhidettsortorder > > ----- > > say "fieldnames = " . @fieldnames; > prints > fieldnames = 7 > > ----- > > say "fieldnames = @fieldnames"; > prints > fieldnames = UID GroupName lastmod categoryhtml hide tt sortorder > > ----- > > Why does @fieldnames give a different result depending on how it's used within > a say statement? Because Perl is smart! It treats arrays differently depending upon the context. In the first case, the say command just prints each element of the array. In the second case, the concatenation operator forces scalar context on the array, and in scalar context the array evaluates to the number of elements in the array. In the third case, Perl places the list separator value as stored in the $" variable between array elements when it interpolates an array into a double-quoted string. You can changmuch e the value of $", which starts with a single space character. You can do the same thing with the join function. Three different contexts; three different results. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/