tag:blogger.com,1999:blog-5907655605807122632.post4631230753076365554..comments2024-03-19T02:17:41.360-07:00Comments on Perl Adventures: @arr = ("my", "first", "array");Anonymoushttp://www.blogger.com/profile/14422352024976746995noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-5907655605807122632.post-5255918841595694892014-02-10T16:52:09.126-08:002014-02-10T16:52:09.126-08:00Ah, OK, I understand now. (I'm a Perl newbie ...Ah, OK, I understand now. (I'm a Perl newbie myself.) Thanks for the clarification, Barney.Verbilahttps://www.blogger.com/profile/17192522257904225548noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-87695145103742500492014-02-08T08:41:45.826-08:002014-02-08T08:41:45.826-08:00To be completely precise, the space is used by def...To be completely precise, the space is used by default, but this can be changed by setting $" variable to a different value. Check this:<br /><br />my @x = (1, 2, 3);<br />print "@x\n";<br />$" = ',';<br />print "@x\n";Szymon Sokółhttps://www.blogger.com/profile/10330483300608404229noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-65462194353665180152014-02-07T16:21:13.881-08:002014-02-07T16:21:13.881-08:00For references I highly recommend reading perlreft...For references I highly recommend reading perlreftut doc page. It makes things really easy. I also recommend never ever looking at the perldsc page. It's a crutch, better to really learn from reftut than to just mimic dsc. Anonymoushttps://www.blogger.com/profile/14862225320062725186noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-88497891125929448012014-02-07T08:34:43.786-08:002014-02-07T08:34:43.786-08:00There is another way to assign to/read from arrays...There is another way to assign to/read from arrays: slices. Slices let you assign to/read from multiple individual elements of the array at the same time. To make a slice you use an @ sigil instead of a $ sigil:<br /><br />my @arr;<br /><br />@arr[3, 1, 2, 0] = ("4", "is", "array", "this");<br /><br />print "@arr\n@arr[reverse 0 .. $#arr]\n";Chas. Owenshttps://www.blogger.com/profile/11716389363199045015noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-34825718291397782014-02-07T01:03:21.672-08:002014-02-07T01:03:21.672-08:00Ah that makes sense, thank youAh that makes sense, thank youAnonymoushttps://www.blogger.com/profile/14422352024976746995noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-21651587070611805922014-02-07T01:01:50.538-08:002014-02-07T01:01:50.538-08:00Oh yeh! Thanks, I will change it nowOh yeh! Thanks, I will change it nowAnonymoushttps://www.blogger.com/profile/14422352024976746995noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-61910447856104071192014-02-06T12:23:22.008-08:002014-02-06T12:23:22.008-08:00Emma wrote it correctly. An array in double quotes...Emma wrote it correctly. An array in double quotes is expanded into it's element joined by a single space. The newline is only printed after the element of the array. Try it yourself.<br /><br />perl -E 'my @arr = ( 1, 2, 3 ); print "@arr\n4";'<br /><br />gives:<br /><br />1 2 3<br />4<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-32906332060449740382014-02-06T11:34:26.257-08:002014-02-06T11:34:26.257-08:00One more tiny nitpick - I think when you wrote you...One more tiny nitpick - I think when you wrote you can find the length of an array with:<br /><br /> $#arr-1<br /><br />You probably meant:<br /><br /> $#arr+1<br /><br />Good stuff - keep it up.Grant McLeanhttps://www.blogger.com/profile/14948907269648277687noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-74790914431913771812014-02-06T11:18:16.634-08:002014-02-06T11:18:16.634-08:00Really enjoying your blog!
On the tip you say tha...Really enjoying your blog!<br /><br />On the tip you say that "each element is separated by a space." But your code has the newline character, not a space character:<br /><br />print "@array\n";Verbilahttps://www.blogger.com/profile/17192522257904225548noreply@blogger.comtag:blogger.com,1999:blog-5907655605807122632.post-53872178926085880102014-02-06T10:06:15.080-08:002014-02-06T10:06:15.080-08:00Sorry to say, but your explanation of the behaviou...Sorry to say, but your explanation of the behaviour of this code:<br /><br />print scalar(100,300,200);<br /><br />is wrong (though I agree that the problem itself *is* quite unclear).<br /><br />The trick is, there is *no* array in this example. Were there an array involved, scalar() would return the number of elements in that array, as you correctly stated a bit earlier. However, in this case there is no array. Thjs list of comma-separated numbers is an expression, and comma acts here not as a separator, but as an operator. And this operator always return its last argument. This (and some other quirks of Perl related to the arrays and the comma) is very well described here:<br /><br />http://www.modernperlbooks.com/mt/2013/11/context-and-the-comma-operator.html<br /><br />much better than I can do anyway, so I'll stop here.Szymon Sokółhttps://www.blogger.com/profile/10330483300608404229noreply@blogger.com