The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by vipinh]

Hi Dave,
I just bought your book from Amazon.com.In the meantime, I've been reading
the sample chapter (Ch 2) I downloaded from this site. On page 6 of Ch 2,
in the "input" subroutine, I cant quite follow the exact syntax of one
particular line; I understand what the subroutine does (create an array of
hashes), but i cant quite follow the exact syntax :

@rec{@attrs} = split / /

Its the left side of the equation i dont fully understand...since rec is a
hash, why is it being prefixed with @ (since @ is for arrays)

I'm not very experienced with Perl (somewhere between a newbie and an
intermediate level)...sorry if the question sounds foolish...

Regards,
Vipin
import-bot (20211) [Avatar] Offline
#2
Re: Query
[Originally posted by dave]

It's called a hash slice. And it's a very useful construct.

You know that you can access a single hash element using the syntax
$hash{$key}. You can use this syntax either on the right hand side of an
assignment (where it gives you the associated value):

$value = $hash{$key};

or on the left hand side, where it gives you a hash "slot" to put data:

$hash{$key} = $new_value;

The $ sign at the front of the $hash{$key} tells you that you are talking
about a single value or slot.

But you can access more than one hash element at the same time. This is the
hash slice. As you've seen, the syntax is @hash{@list_of_keys}. The @ sign
tells us that we're refering to a number of hash elements. As with the single
element, we can using it on the right hand side of an assignment (to get a
list of values):

@values = @hash{@list_of_keys};

or on the left hand side to get a list of slots to store values:

@hash{@list_of_keys} = @list_of_new_values;

Look up hash slices in any good Perl book for more information.

By the way, you can do the same thing with arrays as well.

hth,

Dave...
import-bot (20211) [Avatar] Offline
#3
Re: Query
[Originally posted by vipinh]

Hi Dave,
Thanks for the very lucid explanation...it's quite clear to me now.

P.S. Sorry about that earlier blank post...submitted the form by mistake...