Different results for say @array
Different results for say @array
am 08.06.2011 19:08:53 von sono-io
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 =3D " . @fieldnames;
prints
fieldnames =3D 7
-----
say "fieldnames =3D @fieldnames";
prints
fieldnames =3D UID GroupName lastmod categoryhtml hide tt sortorder
-----
Why does @fieldnames give a different result depending on how =
it's used within a say statement?
Thanks,
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Different results for say @array
am 08.06.2011 19:22:19 von Octavian Rasnita
From:
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 =3D " . @fieldnames;
prints
fieldnames =3D 7
-----
say "fieldnames =3D @fieldnames";
prints
fieldnames =3D UID GroupName lastmod categoryhtml hide tt sortorder
-----
Why does @fieldnames give a different result depending on how it's used =
within a say statement?
Thanks,
When you quote an array, the elements of that array will be separated by =
the content of variable $".
Try putting this line before print():
$" =3D '';
When you just print the array without quoting it, the elements of the =
array are separated by the content of the variable $,
Try putting the following line before print():
$, =3D ' ';
You will see that now the elements of the array printed between quotes =
are not separated by space, while the elements of the array printed =
without quotes are separated by spaces.
Of course, you can use anything for $, and $" variables, not only =
spaces.
When you access an array in scalar context, you get the number of =
elements of that array. It is just like saying:
$nr_of_elements =3D scalar @array;
When you do:
print @array;
you access @array in list context, because the function print() expects =
a list.
When you do:
print "something" . @array;
you access @array in scalar context, because the operator "." expects a =
scalar...
HTH.
Octavian
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Different results for say @array
am 08.06.2011 19:25:36 von Jim Gibson
On 6/8/11 Wed Jun 8, 2011 10:08 AM, "sono-io@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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Different results for say @array
am 08.06.2011 19:43:51 von Uri Guttman
others have given you answers for each case. but no one has clarified an
important misconception you seem to have. say has nothing to do with the
results you are seeing. it is all about context in expressions. if you
don't understand context, then you can't code perl well as context is
all over the place. for arrays, scalar vs list context is
key. interpolating an array is actually described in perldoc perldata
and is different than just context. in none of those cases does using
say matter. you can get the same results by assigning the expressions
(in the same way you did with say) and then printing them. so learn to
isolate an expression from the function you are using. this is a classic
newbie problem. another common example is when newbies call the here doc
<< operator a part of print since it seems to be used often with
print. you assumed these were all different issues with say and it
wasn't.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Different results for say @array
am 08.06.2011 22:45:40 von sono-io
On Jun 8, 2011, at 10:25 AM, Jim Gibson wrote:
> Because Perl is smart! It treats arrays differently depending upon the =
context.
On Jun 8, 2011, at 10:22 AM, Octavian Rasnita wrote:
> When you quote an array, the elements of that array will be separated =
by the content of variable $".
Thanks guys. Very interesting information.
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/