YUANya

导航

 
#!/usr/bin/perl

use strict;
use warnings;

my %fruit;
$fruit{'ap'.'ple'} = 1;
$fruit{'banana'} = 2;
$fruit{'peach'} = 3;

foreach my $choice (qw(apple banana peach x))
{
    print "$fruit{$choice}\n"
}

#1
#2
#3
#Use of uninitialized value within %fruit in concatenation (.) or string at test.pl line 13.
#

my %letter = ('a',1,'b',2,'c','c1');

foreach my $key (keys %letter)
{
    print "$letter{$key}\n";
}

#2
#1
#c1

my @arr = %letter;print "@arr\n";

#c c1 b 2 a 1

my %new = %letter;

foreach my $key (keys %new)
{
    print "$new{$key}\n";
}

#1
#c1
#2

my %reverse = reverse %letter;

foreach my $key (keys %reverse)
{
    print "$reverse{$key}\n";
}

#b
#c
#a

my %test = ('a' => 1,
            'b' => 2,
            'c' => 1,
            );

my %reverse_test = reverse %test;

foreach my $key (keys %reverse_test)
{
    print "$reverse_test{$key}\n";
}

#b
#c
#b

my @k = keys %test;print "@k\n";
my @v = values %test;print "@v\n";

#c a b
#1 1 2

if(%test){print "r!\n"}

#r!

$test{'d'} = 0;
$test{'e'} = undef;

while(my($k,$v) = each %test)
{
    print "$k => $v\n";
}

#Use of uninitialized value $v in concatenation (.) or string at test.pl line 90.
#e =>
#d => 0
#b => 2
#c => 1
#a => 1

if(exists $test{'r'})
{
    print "get!\n";
}
else
{
    print "nothing!\n";
}

#nothing!

if(exists $test{'e'})
{
    print "get!\n";
}
else
{
    print "nothing!\n";
}

#get!

delete $test{'e'};

if(exists $test{'e'})
{
    print "get!\n";
}
else
{
    print "nothing!\n";
}

#nothing!

print "%test\n";

#%test

print "$ENV{PATH}\n";
posted on 2019-06-16 19:30  YUANya  阅读(115)  评论(0编辑  收藏  举报