Learning Perl:

Previous Page

Next Page

 

6.2. Hash Element Access

To access an element of a hash,use syntax that looks like this:

    $hash{$some_key}

This is similar to what we used for array access,but here we use curly braces instead of square brackets around the subscript (key).[*] That key expression is now a string,rather than a number:

[*] Here's a peek into the mind of Larry Wall: Larry says that we use curly braces instead of square brackets because we're doing something fancier than ordinary array access,so we should use fancier punctuation.

    $family_name{"fred"} = "flintstone";
    $family_name{"barney"} = "rubble";

Figure 6-3 shows how the resulting hash keys are assigned.

 

Figure 6-3. Assigned hash keys

 


This lets us use code like this:

    foreach $person (qw< barney fred >) {
      print "I've heard of $person $family_name{$person}./n";
    }

The name of the hash is like any other Perl identifier (letters,digits,and underscores,but it can't start with a digit). And it's from a separate namespace; that is,there's no connection between the hash element $family_name{"fred"} and a subroutine &family_name,for example. Of course,there's no reason to confuse everyone by giving everything the same name. But Perl won't mind if you have a scalar called $family_name and array elements like $family_name[5]. We humans will have to do as Perl does; we'll have to look to see what punctuation appears before and after the identifier to see what it means. When there is a dollar sign in front of the name and curly braces afterward,it's a hash element that's being accessed.

When choosing the name of a hash,it's often nice to think of the word "for" between the name of the hash and the key. As in,"the family_name for fred is flintstone." So,the hash is named family_name,and the relationship between the keys and their values becomes clear.

The hash key may be any expression and not just the literal strings and scalar variables that we're showing here:

    $foo = "bar";
    print $family_name{ $foo . "ney" };  # prints "rubble"

When you store something into an existing hash element,it overwrites the previous value:

    $family_name{"fred"} = "astaire";  # gives new value to existing element
    $bedrock = $family_name{"fred"};   # gets "astaire"; old value is lost

That's analogous to what happens with arrays and scalars; if you store something new into $pebbles[17] or $dino,the old value is replaced. If you store something new into $family_name{"fred"},the old value is replaced as well.

Hash elements spring into existence by assignment:

    $family_name{"wilma"} = "flintstone";             # adds a new key (and value)
    $family_name{"betty"} .= $family_name{"barney"};  # creates the element if needed

That's like what happens with arrays and scalars; if you didn't have $pebbles[17] or $dino before,you will have it after you assign to it. If you didn't have $family_name{"betty"} before,you do now.

Accessing outside the hash gives undef:

    $granite = $family_name{"larry"};  # No larry here: undef

This is like what happens with arrays and scalars; if there's nothing yet stored in $pebbles[17] or $dino,accessing them will yield undef. If there's nothing yet stored in $family_name{"larry"},accessing it will yield undef.

6.2.1. The Hash as a Whole

To refer to the entire hash,use the percent sign ("%") as a prefix. The hash we've been using for the last few pages is actually called %family_name.

For convenience,a hash may be converted into a list and back again. Assigning to a hash (in this case,the one from Figure 6-1) is a list-context assignment,where the list is made of key/value pairs:[*]

[*] Though any list expression may be used,it must have an even number of elements because the hash is made of key/value pairs. An odd element will likely do something unreliable,though it's a warnable offense.

    %some_hash = ("foo",35,"bar",12.4,2.5,"hello","wilma",1.72e30,"betty","bye/n");

The value of the hash (in a list context) is a list of key/value pairs:

    @any_array = %some_hash;

We call this unwinding the hashturning it back into a list of key/value pairs. The pairs won't necessarily be in the same order as the original list:

    print "@any_array/n";
      # might give something like this:
      #  betty bye (and a newline) wilma 1.72e+30 foo 35 2.5 hello bar 12.4

The order is jumbled because Perl keeps the key/value pairs in an order that's convenient for Perl so it can look up any item quickly. You use a hash when you don't care what order the items are in or when you have an easy way to put them into the order you want.

Though the order of the key/value pairs is jumbled,each key "sticks" with its corresponding value in the resulting list. So,though we don't know where the key foo will appear in the list,we know that its value,35,will be right after it.

6.2.2. Hash Assignment

It's rare to do so,but a hash may be copied using this syntax:

    %new_hash = %old_hash;

This is more work for Perl than meets the eye. Unlike what happens in languages like Pascal or C,where such an operation would copy a block of memory,Perl's data structures are more complex. So,that line of code tells Perl to unwind the %old_hash into a list of key/value pairs and assign those to %new_hash,building it up one key/value pair at a time.

Transforming the hash in some way is more common. For example,we could make an inverse hash:

    %inverse_hash = reverse %any_hash;

This takes %any_hash and unwinds it into a list of key/value pairs,making a list like (key,value,key,...). Then,reverse turns that list end-for-end,making a list like (value,...). The keys are where the values used to be,and the values are where the keys used to be. When that's stored into %inverse_hash,we'll be able to look up a string that was a value in %any_hash,because it's now a key of %inverse_hash. And the value we'll find is one that was one of the keys from %any_hash. This gives us a way to look up a "value" (now a key),and find a "key" (now a value).

You might guess (or determine from scientific principles if you're clever) that this will work properly only if the values in the original hash are unique; otherwise,we'd have duplicate keys in the new hash,and keys are always unique. Here's the rule that Perl uses: the last one in wins. That is,the later items in the list overwrite any earlier ones. We don't know what order the key/value pairs will have in this list,so there's no telling which ones will win. You'd use this technique only if you know there are no duplicates among the original values.[*] But that's the case for the IP address and hostname examples given earlier:

[*] Or if you don't care that there are duplicates. For example,we could invert the %family_name hash (in which the keys are people's given names and values are their family names) to make it easy to determine if anyone has a given family name in the group. Thus,in the inverted hash,if there's no key of slate,we'll know that there's no one with that name in the original hash.

    %ip_address = reverse %host_name;

We can look up a hostname or IP address with equal ease to find the corresponding IP address or hostname.

6.2.3. The Big Arrow

When assigning a list to a hash,sometimes it's not obvious which elements are keys and which are values. For example,in this assignment (which we saw earlier),we have to count through the list,saying,"key,value,key,value..." to determine whether 2.5 is a key or a value:

    %some_hash = ("foo","bye/n");

Wouldn't it be nice if Perl gave us a way to pair up keys and values in that kind of a list so we could easily see see which ones were which? Larry thought so,too,which is why he invented the big arrow,(=>).[*] To Perl,it's just a different way to "spell" a comma,so it's also sometimes called the "fat comma." In the Perl grammar,any time that you need a comma ( , ),you can use the big arrow instead; it's all the same to Perl.[

] Here's another way to set up the hash of last names:

[*] Yes,there's also a little arrow,(->). It's used with references,which are an advanced topic. See the perlreftut and perlref manpage when you're ready for that.

[

] Well,there's one technical difference: any bareword (a sequence of nothing but letters,and underscores not starting with a digit but optionally prefixed with plus or minus) to the left of the big arrow is implicitly quoted. So you can omit the quote marks on a bareword to the left of the big arrow. You may also omit the quote marks if there's nothing but a bareword as a key inside the curly braces of a hash.

    my %last_name = (  # a hash may be a lexical variable
      "fred"   => "flintstone","dino"   => undef,"barney" => "rubble","betty"  => "rubble",);

Here,it's easy (or perhaps at least easier) to see whose name pairs with which value even if we end up putting many pairs on one line. And notice the extra comma at the end of the list. As we saw earlier,this is harmless but convenient; if we need to add additional people to this hash,we'll simply make sure that each line has a key/value pair and a trailing comma. Perl will see that there is a comma between each item and the next,and one extra (harmless) comma at the end of the list.

Previous Page

Next Page

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(&lt;&gt;){ chomp; print &quot;$_n&quot; unless
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据进行拼凑,然后将拼凑后的数据用“|”连接在一起。表格内容如下: 员工号码员工姓名职位入职日期1001张三销售1980/12/17 0:00:
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确。写了个Perl脚本,可快速构造MySQL脚本语句。脚本如下:#!/usr/bin/perluse strict;m
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了一个表的数据量统计日报,主要是针对数据库中使用较频繁,数据量又较大的31张表。该日报有两个sheet组成,第一个sheet是数据填写,第二个sh
在实际生产环境中,常常需要从后台日志中截取报文,报文的形式类似于.........一个后台日志有多个报文,每个报文可由操作流水唯一确定。以前用AWK写过一个,程序如下:beginline=`awk &
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让人领略到perl正则表达式的强大,程序如下:#!/usr/bin/perluse strict;my (%hash,%hash1,@arra
忍不住在 PerlChina 邮件列表中盘点了一下 Perl 里的 Web 应用框架(巧的是 PerlBuzz 最近也有一篇相关的讨论帖),于是乎,决定在我自己的 blog 上也贴一下 :) 原生 CGI/FastCGI 的 web app 对于较小的应用非常合适,但稍复杂一些就有些痛苦,但运行效率是最高的 ;) 如果是自己用 Perl 开发高性能的站,多推荐之。 Catalyst, CGI::A
bless有两个参数:对象的引用、类的名称。 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键。 所谓bless就是把 类型信息 赋予 实例变量。 程序包括5个文件: person.pm :实现了person类 dog.pm :实现了dog类 bless.pl : 正确的使用bless bless.wrong.pl : 错误的使用bless bless.cc : 使用C++语言实
gb2312转Utf的方法: use Encode; my $str = "中文"; $str_cnsoftware = encode("utf-8", decode("gb2312", $str));   Utf转 gb2312的方法: use Encode; my $str = "utf8中文"; $str_cnsoftware = encode("gb2312", decode("utf-8
  perl 计算硬盘利用率, 以%来查看硬盘资源是否存在IO消耗cpu资源情况; 部份代码参考了iostat源码;     #!/usr/bin/perl use Time::HiRes qw(gettimeofday); use POSIX; $SLEEPTIME=3; sub getDiskUtl() { $clock_ticks = POSIX::sysconf( &POSIX::_SC_
1 简单变量 Perl 的 Hello World 是怎么写的呢?请看下面的程序: #!/usr/bin/perl print "Hello World" 这个程序和前面 BASH 的 Hello World 程序几乎相同,只是第一行换成了 #!/usr/bin/perl ,还有显示的时候用的是 print,而不是 echo。有了前面 BASH 基础和 C 语言的基础,许多 Perl 的知识可以很
本文介绍Perl的Perl的简单语法,包括基本输入输出、分支循环控制结构、函数、常用系统调用和文件操作,以及进程管理几部分。 1 基本输入输出 在 BASH 脚本程序中,我们用 read var 来实现从键盘的输入,用 echo $var 来实现输出。那么在 Perl 中将有一点变化。Perl 中将标准输入用关键词 表示;标准输出用 表示,标准错误输出用 表示。故而从标准输入读取数据可以写成: $
正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果大家能够很好的掌握他,就可以轻易地用正则表达式来完成字符串处理的任务,当然在 CGI 程序设计中就更能得心应手了。下面我们列出一些正则表达式书写时的一些基本语法规则。 1 正则表达式的三种形式 首先我们应该知道 Perl 程序中,正则表达式有三种存在形式,他们分别是: 匹配:m/<regexp>/ (还可以简写为 /
在学习Perl和Shell时,有很多人可能会问这样一个问题,到底先学习哪个或者学习哪个更好! 每个人都有自己的想法,以下是个人愚见,请多多指教! Perl是larry wall为解决日常工作中的一个编程问题而产生的,它最初的主要功能是用于分析基于文本的数据和生成这些数据的统计和结果;尽管初衷很简单,但是后来发展了很多特点: 1、Perl是一种借鉴了awk、C、sed、shell、C++、Java等
Perl 有很多命令行参数. 通过它, 我们有机会写出更简单的程序. 在这篇文章里我们来了解一些常用的参数. (重点提示:在window下执行命令行程序的方式为 : perl -e "some code", 注意:是双引号啊,不是单引号,linux下执行时单引号) Safety Net Options 在使用 Perl 尝试一些聪明( 或 stupid) 的想法时, 错误难免会发生. 有经验的 P
转自: http://bbs.chinaunix.net/thread-1191868-1-1.html# 让你的perl代码看起来更像perl代码,而不是像C或者BASIC代码,最好的办法就是去了解perl的内置变量。perl可以通过这些内置变量可以控制程序运行时的诸多方面。 本文中,我们一起领略一下众多内置变量在文件的输入输出控制上的出色表现。 行计数 我决定写这篇文章的一个原因就是,当我发现
2009-02-02 13:07 #!/usr/bin/perl # D.O.M TEAM - 2007 # anonyph; arp; ka0x; xarnuz # 2005 - 2007 # BackConnectShell + Rootlab t00l # priv8! # 3sk0rbut0@gmail.com # # Backconnect by data cha0s (modifica
转自: http://bbs.chinaunix.net/thread-1191868-1-1.html# 让你的perl代码看起来更像perl代码,而不是像C或者BASIC代码,最好的办法就是去了解perl的内置变量。perl可以通过这些内置变量可以控制程序运行时的诸多方面。 本文中,我们一起领略一下众多内置变量在文件的输入输出控制上的出色表现。 行计数 我决定写这篇文章的一个原因就是,当我发现
黑莓 手机 屏幕发展历程对比 blackberry 各型号屏幕大小   黑莓手 机 一直在不断发展且新机型 也在不断上市. 因此,不同黑莓机型的屏幕分辨率也在不断变化着. 总的来说,屏幕分辨率一直在提高并且越来越清晰.我们对所有的黑莓 机型的屏幕分辨率做了个对比.~51blackberry ~com     可能大家特别感兴趣是新发布的黑莓机型,它的分辨率也是黑莓 机型中前所未有的.   黑莓 b
      公司里没有我用惯的UltraEdit的lisence了, 只能无奈转向开源的Notepad++, 找了半天才知道配置运行Perl的办法。         1,用Notepad++打开.pl文件,         2, F5或者Run->Run,打开运行窗口,在下面的框框里输入:Perl -w "$(FULL_CURRENT_PATH)", 然后Save,保存成一个命令就行,名字比如叫R