perl学习笔记(十九)切片操作

文章目录


前言

本文主要记录一下,perl中的切片操作。包括:

  • 数组切片
  • 哈希切片

19 切片

#=====================================
# 数组切片
#=====================================
while(<>){
    chomp;
    #方案1:
    my @items = split /:/; #split按照:进行切分
    my($card_num, $count) = ($items[1], $items[5]);
    #方案2:
    my (undef,$card_num, undef, undef, undef, $count) = split /:/;
    #方案3:切片
    my $card_num = (split /:/)[1];
    my $count = (split /:/)[5];
    #方案4:切片简化
    my($card_num, $count) = (split /:/)[1,5];
    print "$card_num, $count\n";
}

my @num = qw(0 1 2 3 4 5 6 7 8 9);
#切片提取
my ($first, $last) = @num[0,-1];
print "$first, $last\n"; #0, 9
#切片修改
@num[0,-1] = ("zero", "nine");
print "@num\n"; #zero 1 2 3 4 5 6 7 8 nine


#=====================================
# 哈希切片
#=====================================
#普通获取hash数据
my %scores = (0 => "zero", 1 => "one", 2 => "two", 3 => "three");
my @num = ($scores{0}, $scores{1}, $scores{2});
print "@num \n";

#hash切片获取hash数据
my @nums = @scores{qw/0 1 2/};
print "@nums \n";

#hash切片修改hash数据
my @values = qw/3 4 5/;
@scores{qw/0 1 2/} = @values;
while(my($k,$v) = each(%scores)){
    print "$k => $v\n";
}
#3 => three
#2 => 5
#0 => 3
#1 => 4

总结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值