On Jul 19, Robert Citek said:

On Jul 19, 2005, at 5:19 PM, Wiggins d'Anconia wrote:
Close. You want a hash slice.

@[EMAIL PROTECTED] = @vals;

A marvelously Perlish construct.

http://danconia.org

Sweeeeet!  Thanks a bunch.

Do you have a good reference which explains this? I've looked in both the Camel and Llama books, but didn't see the @$ construct anywhere.

Well, let's take a step back. You don't have a *hash*, per se, you have a reference to a hash.

  $hash->{x} = $y;

implies (rightly so) that $hash is a reference to a hash. If you had a hash without the reference, you would have written

  $hash{x} = $y;

So let's deal with that for the moment. Slices are ways to access a list of values at one time (whether for reading or assigning). Here are single array and hash accesses:

  $array[3] = 10;
  $hash{name} = 'japhy';

and here are array and hash slices:

  @array[3,5,7] = (10, 15, 20);
  # same as ($array[3], $array[5], $array[7]) = (10, 15, 20)
  @hash{'name', 'state'} = ('japhy', 'NJ');
  # same as ($hash{name}, $hash{state}) = ('japhy', 'NJ')

You can use slices on the right-hand side of an = as well:

  @pair = @array[0..1];
  @contact = @hash{'phone', 'email'};

So now. What's the @$hash{...} syntax? Well, like I said, you don't have just a hash, you have a hash reference. In the place of the *name* of the hash in '$HASHNAME{...}' and '@HASHNAME{...}', you can put a reference; thus: '$$hashref{...}' and '@$hashref{...}'.

The first one, $$hashref{...}, is more commonly written as you wrote it: $hashref->{...}. But for the second one, there is no -> syntax that works. You just have to use @$hashref{...}.

For more, please read

  perldoc perldata

to learn about slices, and

  perldoc perlreftut

to be introduced to references.  Further reading is:

  perldoc perlref
  perldoc perllol
  perldoc perldsc

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to