in reply to Re: passing array ref without variable
in thread passing array ref without variable

Can I pass somehow an array ref of values without separately forming an array? Maybe using foreach...
  • Comment on Re^2: passing array ref without variable

Replies are listed 'Best First'.
Re^3: passing array ref without variable
by kennethk (Abbot) on May 03, 2010 at 16:05 UTC
    Why would you want to? Inline anonymous array construction is a trivial number of characters and is robust. This sounds like an XY Problem.
Re^3: passing array ref without variable
by LanX (Saint) on May 03, 2010 at 17:03 UTC
    I suppose what you really want/need is to pass the hash as reference, like this you don't need to pass the values and the keys separately.

     call(\%hash)

    If you explicitly want to pass the reference of a copy do this

     call( { %hash } )

    the { ... } returns a ref of an anonymous hash of the list expanded from %hash, i.e. the ref of a hashcopy!

    Cheers Rolf

      No I do not want to pass a hash. What I want to do is having a hash ($h{key} = value) and
      sub my_function { my $arr_ref = shift; ... }
      I can pass keys as an array ref by
      my_function([keys %hash])
      Now, I want to pass values as an array ref in a shortest way without forming an array. Is it possible?
        Hmmm.

        You know that you get the keys of a hash using a function called keys.

        What do you think would be the name of the function that returns the values of a hash?

        Is it possible?

        Short answer, yes!

         my_function([values %hash])

        but please be aware that this is only a flat copy.

        Cheers Rolf

Re^3: passing array ref without variable
by AnomalousMonk (Archbishop) on May 03, 2010 at 16:15 UTC

    Why would you want to? And how could you? It sounds like you (vit) are asking "Can I take a reference to an array that does not exist?".