Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Conditional array initialisation?

by LanX (Canon)
on Jul 12, 2013 at 19:38 UTC ( #1044063=note: print w/ replies, xml ) Need Help??


in reply to Conditional array initialisation?

I suppose you want to assign default values to empty arrays only ("doesn't work" ???)

Using defined doesn't make sense here (see also hdb's reply), and the other expression has to be analogous to

DB<125> @a=() DB<126> @a = (@a || ( 1..3 )); => (1, 2, 3) DB<127> @a = (@a || ( 4..6 )); => 3

But the second case fails cause the LHS of an or operator is evaluated in scalar context.

We had a longer discussion about this some weeks ago, which I didn't really follow (maybe somenone can link to it)

FWIW this works:

DB<138> @a=() DB<139> @a = (4..6) if !@a => (4, 5, 6) DB<140> @a=() DB<141> @a = @a ? @a : ( 1..6 ) ; => (1, 2, 3, 4, 5, 6) DB<142> @a = @a ? @a : ( 42 ) ; => (1, 2, 3, 4, 5, 6) DB<143> @a = (4..6) if !@a => "" DB<144> \@a => [1, 2, 3, 4, 5, 6]

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: Conditional array initialisation?
Select or Download Code
Re^2: Conditional array initialisation?
by BrowserUk (Pope) on Jul 12, 2013 at 19:58 UTC
    Using defined doesn't make sense here

    Why not?

    @used = 1; say defined @used ? '1' : '0'; 1 say defined @neverUsed ? '1' : '0';; 0

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    /div
      The old use of defined @arr is deprecated, cause the logic was broken.

      For me undefined means "not initialized" (yet), like with  my $a. But this state doesn't exist for array and hashes, they are initialized empty.

      And if it existed it would be different from (defined and) empty.

      Nevertheless undef @a is allowed, adding even more confusion:

      DB<153> @a => (1, 2, 3, 4, 5, 6) DB<154> undef @a => undef DB<155> \@a => []

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Nevertheless undef @a is allowed, adding even more confusion:

        I don't see any confusion nor potential for it.

        If I have a scalar and at some point in my code I wish to check if it currently has a value, and if it does not, set it, I might write:

        my $x; ... $x = getValue() unless defined $x;

        More recently, I'd write that as: $x //= getValue();. Clear concise semantics.

        If have an array, and at some point in my code I want to check if it contains anything, and populate it if it does not, I might write:

        my @a; ... @a = getValues() unless @a;

        Thus using the value of the array in a scalar context to decide if the array is empty or not.

        It seems a natural, semantically clear, easily implemented extension of the scalar case above to write that as:

        @a //= getValues();

        Test the scalar context value of the array, and if it is false, assign the list on the right hand side to the array on the left.

        Indeed, it seemed so logically analogous, and semantically clear, that I wrote it and expected it to work, and I was taken by considerable surprise that it didn't.

        Hence my OP.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044063]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-21 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls