Shlomi Fish <shlo...@shlomifish.org> writes:

> Hi lee,
>
> I should note that it is my impression that you are far too needy in your
> enquiring, and I'm starting to lose patience.
>
> On Mon, 25 Jan 2016 00:11:43 +0100
> lee <l...@yagibdah.de> wrote:
>
>> Shlomi Fish <shlo...@shlomifish.org> writes:
>> 
>> >> >
>> >> > In scalar context the comma operator evaluates its left-hand side,
>> >> > throws it away and returns the right-hand side.    
>> >> 
>> >> What is the useful use for this operator?
>> >>   
>> >
>> > Well, I believe its use was originally inherited from
>> > https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
>> > something like:
>> >
>> >    x = (y++, y+2);
>> >
>> > In Perl 5 though it is preferable to use do { ... } instead:
>> >
>> >    $x = do { $y++; $y+2; };
>> >
>> > See http://perldoc.perl.org/functions/do.html . GCC and compatible 
>> > compilers
>> > have a similar feature to Perl 5's do {...} called statement expressions:
>> > https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html .  
>> 
>> How's that useful?  Isn't that equivalent to
>> 
>> $x = $y + 3;
>> 
>> ?
>
> Well, it also changes the value of $y. You can use more complicated examples,
> and this was just for the sake of demonstration.

$x = $y++ + 2;

All versions are convoluted code.

> One common pattern I used is to do:
>
> my %cache;
> sub f
> {
>
>       return $cache{$result} //= do {
>               # Calculate the cached result here.
>       };
> }

What does that do?

>> >> How do you convert an array into a list?
>> >>   
>> >
>> > You just put it in list context. For example (untested):
>> >
>> > sub f
>> > {
>> >    print (@_);
>> > }
>> >
>> > my @input = (3, 44, 505, 6.6);
>> >
>> > f(@input);
>> >
>> > my @other_list = (5,@input,24);  
>> 
>> I'm not sure where the conversion of an array (non-static) into a list
>> (static) would take place in this example.
>> 
>
> (5,@input,24) becomes (5,3,44,505,6.6,24).

splice (@other_list, 3, 1, 4);

Are you saying that would give you an error in this case for you created
a (partly) static array?

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to