`
sillycat
  • 浏览: 2542840 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Perl Language(III)

阅读更多
Perl Language(III)

5. Hash(Key-Value)
5.2 Hash syntax
my %hash;

my %hash;
$hash{key} = 'value';
$hash{name} = "sillycat";
print $hash{key} . " " . $hash{name};

my $var = 1;
my @var = (1,2,3,4);
my %var;
$var{1} = 2;
$var{2} = 38;
print $var . " " . $var[3] . " " . $var{2}

console output:
1 4 38

my %hash;
for (1...5) {
    $hash{$_*2} = $_**2;
}

equals to

$hash{2} = 1;
$hash{4} = 4;
$hash{6} = 9;
$hash{8} = 16;
$hash{10} = 25;

5.3 put the value in hash
my %hash = qw/1 one 2 two 3 three/;

equals to

$hash{1} = 'one';
$hash{2} = 'two';
$hash{3} = 'three';

equals to

my %hash = (
            1 => 'one',
            2 => 'two',
            3 => 'three',
            );

5.4 each
my %hash = (
            1 => 'one',
            2 => 'two',
            3 => 'three',
            );
           
while (my ($key, $value) = each (%hash)) { 
      print "$key = $value\n";
}

console output:
1 = one
3 = three
2 = two

my %hash = (
            '168.1.0.1' => 'sillycat',
            '192.168.0.2' => 'carl',
            '192.168.1.3' => 'luohuazju',
            ); 
my @hostname;
while (my ($key, $value) = each (%hash)) {
    if ($key =~ /^192/) { 
        push @hostname, $value;
    }
}

print join ",", @hostname;

console output:
luohuazju,carl

5.5 keys and values
my @keys = keys(%hash);

my @values = values(%hash);

my @keys = keys(%hash);
for (@keys) {
    print "$_ => $hash{$_}\n";
}

my %hash = (
            '168.1.0.1' => 'sillycat',
            '192.168.0.2' => 'carl',
            '192.168.1.3' => 'luohuazju',
            ); 

my @keys =  map { $hash{$_} }
            grep { (m/^192/) } keys(%hash);

print join "," ,@keys;

console output:
luohuazju,carl

5.6 operation of hash
5.6.1 exists
my %hash = (
            '168.1.2.1' => 'verdi',
            '192.1.2.2' => 'wagner',
            '168.1.2.3' => 'beethoven',
            );
my $ip = '192.1.2.2';
print "bingo" if ($hash{$ip});

my %hash = (
            'cd' => 2,
            'book' => 10,
            'video' => 0,
            );
my $media = 'video';
print "bingo" if ($hash{$media});

equals

print "exist" if (grep { $_ eq 'video' } keys (%hash));

equals

print "exists" if (exists $hash{video});

5.6.2 delete
delete $hash{video};

6. Sub function
my $num = 12;
print hex($num),"\n"; 
print &hex($num),"\n";

sub hex {
    $num*2;
}

&hello; 
&hello; 
sub hello {
    print "hello\n";
}

6.2 Parameters
&hello("sillycat"); 

sub hello {
    print @_; 
}

&hello("hello", "sillycat");

sub hello {
    my $action = shift @_;
    my $name = shift @_;
    print "$action $name\n"; 
}

console output:
hello sillycat

6.3 Return Value

my $return = &times(4);
print $return;

sub times {
    my $max = shift;
    my $total = 1;
    for (1...$max) {
  $total *= $_;
    }
    return $total;
}
console output: 24

6.4 More Parameters
my $return = &div(4, 2); 
print $return;

sub div {
    $_[0]/$_[1]; 
}


references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/

分享到:
评论

相关推荐

    Perl Language Reference Manual

    Perl语言参考手册是Perl编程语言的权威文档,提供了关于Perl版本5.12.1的详细信息,这本手册由Perl的创始人Larry Wall和团队编写,并由Network Theory Ltd出版。Perl作为一种高级、通用、解释型、动态编程语言,自...

    The Perl Programming Language

    ### Perl编程语言知识点详解 #### 一、引言 Perl是一种功能强大且灵活的脚本语言,被广泛应用于系统管理任务、网络编程、文本处理等多个领域。本文将深入探讨Perl的基础知识及其在网络与Web开发中的应用,并通过...

    perl Language

    它的名称“Perl”最初是“Practical Extraction and Reporting Language”的首字母缩写,但后来被理解为“Pretty Excellent Reporting Language”或“Pathologically Eclectic Rubbish Lister”。Perl因其强大的文本...

    Perl language

    ### Perl语言:编程界的多面手 #### 概览与历史背景 Perl语言,自诞生以来,便以其独特的魅力在编程领域占据了一席之地。它最初由Larry Wall于1987年创建,旨在解决系统管理和文本处理中的复杂问题。随着版本4在...

    PERL。PERL。ADD

    Perl,全称为"Practical Extraction and Reporting Language",它以灵活、强大和可移植性而闻名。Perl语法混合了C、sed、awk等语言的特点,因此对于熟悉这些语言的开发者来说,学习Perl相对容易。Perl的强大之处在于...

    Modern.Perl.4th.Ed

    You'll understand how the language fits together and discover the secrets used by the global Perl community. This beloved guide is now completely updated for Perl 5.22. When you have to solve a ...

    Perl Data Language-开源

    Perl Data Language(PDL)是一种在Perl编程语言中处理大型多维数组的库,它使得科学家、工程师以及数据分析师能够方便地进行数值计算。PDL的设计目标是将Perl的灵活性和脚本语言的易用性与高效处理大量数据的能力...

    strawberry-perl-5.10.1.0.msi

    Perl,全称为“Practical Extraction and Reporting Language”,是一种高级、通用的解释型、动态编程语言,尤其在文本处理和系统管理领域表现出色。Perl自1987年发布以来,因其强大的功能和灵活性,受到了广大...

    Perl编程思想 Perl编程思想

    Perl,全称“Practical Extraction and Reporting Language”,最初由Larry Wall设计,旨在处理文本处理、报告生成等任务,后来逐渐发展成为一个功能强大的通用编程语言。 一、Perl的设计哲学 1. 工具箱哲学:Perl...

    perl-5.8.8-38.el5.x86_64.rpm

    Perl is a high-level programming language with roots in C, sed, awk and shell scripting. Perl is good at handling processes and files, and is especially good at handling text. Perl's hallmarks are ...

    Perl.6.Essentials

    Description: Perl 6 Essentials is a sneak-preview of Perl 6, the widely-anticipated rewrite of the Perl programming language. Still in development, the Perl 6 project is a community-based effort to ...

    ActivePerl-5_ActivePerl5.28_ActivePerl_源码.zip

    Perl,全称“Practical Extraction and Reporting Language”,是一种高级、通用、解释型、动态的编程语言。Perl最初设计用于文本处理,后来发展成为一种通用的编程语言,广泛应用于系统管理、网络编程、图形编程等...

    Perl-5.18.2

    Perl是一种高级的、通用的、解释型的、动态的编程语言,它的全称是 Practical Extraction and Reporting Language,中文常称为“珍珠”。标题“Perl-5.18.2”指的是Perl语言的一个特定版本,即5.18.2。这个版本在...

    Perl入门书籍——Perl 24小时

    - **名称来源**:Perl的全称最初是Practical Extraction and Report Language,但现在更多地被简称为Perl,体现其轻松和非正式的特点。 - **发展历史**:Perl的许多特性借鉴自其他语言,曾因早期的杂糅特性而被...

    Learning Perl Intermediate Perl Mastering.Per Programming Perl 4本合集 英文 非扫描版

    Perl 一般被称为“实用报表提取语言”(Practical Extraction and Report Language),你也可能看到“perl”,所有的字母都是小写的。一般,“Perl”,有大写的 P,是指语言本身,而“perl”,小写的 p,是指程序...

    perl.zip、perl-5.16.3-295.el7.x86_64.rpm

    它的名字是“Practical Extraction and Reporting Language”的首字母缩写,反映了其在数据提取和报告生成上的优势。 在CentOS 7.6这样的Linux系统上安装Perl,通常我们会使用包管理器如yum或dnf。但在这个案例中,...

    beginning perl for bioinformatics

    programming biologist who needs to solve very specific problems., Beginning Perl for Bioinformatics is designed to get you quickly over the Perl language barrier by approaching programming as an ...

    perl-5.24.1.tar.gz

    Perl是一种高级的、通用的、解释型的、动态的编程语言,它的全称是 Practical Extraction and Reporting Language,即“实用提取和报告语言”。标题中的"perl-5.24.1.tar.gz"指的是Perl的一个特定版本,5.24.1,这个...

    Perl大骆驼.Perl语言入门

    Perl,全称“Practical Extraction and Reporting Language”,是一种高级的、通用的、解释型的、动态的编程语言。因其原始设计者Larry Wall喜欢骆驼,所以Perl的官方吉祥物是一只大骆驼,这也体现在“Perl大骆驼”...

    perl 24小时教程

    Perl,全称“ Practical Extraction and Report Language”,是一种高级的、通用的、解释型的、动态的编程语言。Perl因其强大的文本处理能力而被广泛应用于系统管理、网络编程、数据库交互和Web开发等领域。本“Perl...

Global site tag (gtag.js) - Google Analytics