Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



11.3. Taking References to Hashes


11.3.1. Problem



You need to manipulate a hash by reference.
This might be because it was passed into a function that way or
because it's part of a larger data
structure.

11.3.2. Solution


To get a hash reference:

$href = = { "key1" => "value1", "key2" => "value2", ... };
$anon_hash_copy = { %hash };

To dereference a hash reference:

%hash = %$href;
$value = $href->{$key};
@slice = @$href{$key1, $key2, $key3}; # note: no arrow!
@keys = keys %$href;

To check whether something is a hash reference:

if (ref($someref) ne "HASH") {
die "Expected a hash reference, not $someref\n";
}

11.3.3. Discussion


This example prints out all keys and values from two predefined
hashes:

foreach $href ( \%ENV, \%INC ) { # OR: for $href ( \(%ENV,%INC) ) {
foreach $key ( keys %$href ) {
print "$key => $href->{$key}\n";
}
}

Access slices of hashes by reference as you'd access slices of arrays
by reference. For example:

@values = @$hash_ref{"key1", "key2", "key3"};
for $val (@$hash_ref{"key1", "key2", "key3"}) {
$val += 7; # add 7 to each value in hash slice
}

11.3.4. See Also


The Introductionin Chapter 5; Chapter 8 of
Programming Perl;
perlref(1); Recipe 11.9



11.2. Making Hashes of Arrays11.4. Taking References to Functions




Copyright © 2003 O'Reilly & Associates. All rights reserved.

/ 875