Perl 排序和正则表达式 [英] Perl sorting and regular expressions

查看:50
本文介绍了Perl 排序和正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含如下值的数组:

<前>@数组 =(2014 电脑显示器 200",《2010键盘30》,《2012键盘80》,2011 学习桌 100");

我将如何在 Perl 中使用正则表达式按年份、项目名称和价格对整个数组进行排序?例如,如果用户想按价格排序,他们输入价格",然后排序如下:

<前>2010 键盘 302012键盘802011 学习桌 1002014 电脑显示器 200

到目前为止,我已经能够像这样按年份排序:

<前>@数组 =(2014 电脑显示器 200",《2010键盘30》,《2012键盘80》,2011 学习桌 100");$输入 = ;chomp($input);if ($input eq "年"){foreach $item (sort {$a cmp $b} @array){打印 $item ."\n";}}

解决方案

/(\d+) \s+ (.+) \s+ (\S+)/x 将匹配年份名称和价格,

使用严格;使用警告;我的 $order = "价格";我的@array = (2014电脑显示器200强",《2010键盘30》,《2012键盘80》,2011 学习桌 100");我的 %sort_by = (年 =>sub { $a->{year} <=>$b->{年} },价格 =>sub { $a->{price} <=>$b->{价格} },名称 =>sub { $a->{name} cmp $b->{name} },);@array = 排序 {本地 ($a, $b) = 地图 {我的 %h;@h{qw(年名价格)} =/(\d+) \s+ (.+) \s+ (\S+)/x;\%H;} ($a, $b);$sort_by{$order}->();} @大批;# S. 变换# @数组 =# map { $_->{line} }# sort { $sort_by{$order}->() }#  地图 {# 我的 %h = (line => $_);# @h{qw(年名价格)} =/(\d+) \s+ (.+) \s+ (\S+)/x;# $h{name} ?\%H : ();#  } @大批;使用数据::倾销者;打印转储器 \@array;

输出

$VAR1 = ['2010 键盘 30','2012 键盘 80','2011 学习桌 100','2014 电脑显示器 200'];

I have an array that contains values like this:

@array = 
("2014 Computer Monitor 200",
"2010 Keyboard 30",
"2012 Keyboard 80",
"2011 Study Desk 100");

How would I use regular expressions in Perl to sort the entire array by year, item name, and price? For example, if the user wants to sort by price they type 'price' and it sorts like this:

    2010 Keyboard 30
    2012 Keyboard 80
    2011 Study Desk 100
    2014 Computer Monitor 200

So far I've been able to sort by year like this:

    @array = 
    ("2014 Computer Monitor 200",
    "2010 Keyboard 30",
    "2012 Keyboard 80",
    "2011 Study Desk 100");
    
    $input = ;
    
    chomp($input);
    if ($input eq "year")
    {
        foreach $item (sort {$a cmp $b} @array)
        {
        print $item . "\n";
        }
    }

解决方案

/(\d+) \s+ (.+) \s+ (\S+)/x will match year name and price,

use strict;
use warnings;

my $order = "price";
my @array = (
  "2014 Computer Monitor 200",
  "2010 Keyboard 30",
  "2012 Keyboard 80",
  "2011 Study Desk 100"
);

my %sort_by = (
  year  => sub { $a->{year}  <=> $b->{year} },
  price => sub { $a->{price} <=> $b->{price} },
  name  => sub { $a->{name}  cmp $b->{name} },
);
@array = sort {

  local ($a, $b) = map {
    my %h; 
    @h{qw(year name price)} = /(\d+) \s+ (.+) \s+ (\S+)/x;
    \%h;
  } ($a, $b);
  $sort_by{$order}->();

} @array;

# S. transform
# @array =
#  map { $_->{line} }
#  sort { $sort_by{$order}->() }
#  map { 
#    my %h = (line => $_); 
#    @h{qw(year name price)} = /(\d+) \s+ (.+) \s+ (\S+)/x;
#    $h{name} ? \%h : ();
#  } @array;

use Data::Dumper; print Dumper \@array;

output

$VAR1 = [
      '2010 Keyboard 30',
      '2012 Keyboard 80',
      '2011 Study Desk 100',
      '2014 Computer Monitor 200'
    ];

这篇关于Perl 排序和正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆