Perl 显示带有 Getopt::Long 特定参数的选定数据

Perl display selected data with a specific argument with Getopt::Long

我有一个输入为这个的文件

Store::ID_AZD|AZD|Category::1314559|Séries
Store::ID_AZD|AZD|Category::1314557|Emissions
Store::ID_AZD|AZD|Category::1314560|Jeunesse
Store::ID_AZD|AZD|Category::1314558|Information
Store::ID_FRA|CHANNEL 2|Category::1294563|Séries
Store::ID_FRA|CHANNEL 2|Category::1294059|Info
Store::ID_FRA|CHANNEL 2|Category::1295062|Magazine
Store::ID_FRA|CHANNEL 2|Category::1300056|Documentaire
Store::ID_FRA|CHANNEL 2|Category::1299056|DIVERTISSEMENT
Store::ID_FRA|CHANNEL 2|Category::1295060|Jeu
Store::ID_FRA|CHANNEL 2|Category::1294058|Sport
Store::ID_FRA|CHANNEL 2|Category::1294562|Culture
Store::ID_GRB|CHANNEL 3|Category::1295063|Séries
Store::ID_GRB|CHANNEL 3|Category::1295059|Info
Store::ID_GRB|CHANNEL 3|Category::1295058|Magazine
Store::ID_GRB|CHANNEL 3|Category::1296557|Documentaire
Store::ID_GRB|CHANNEL 3|Category::1300055|DIVERTISSEMENT
Store::ID_GRB|CHANNEL 3|Category::1294576|Jeunesse
Store::ID_GRB|CHANNEL 3|Category::1294559|Jeu
Store::ID_GRB|CHANNEL 3|Category::1295057|Sport
Store::ID_GRB|CHANNEL 3|Category::1295556|Culture
Store::ID_UKR|CHANNEL 5|Category::1294577|Jeunesse
Store::ID_UKR|CHANNEL 5|Category::1296055|Info
Store::ID_UKR|CHANNEL 5|Category::1295061|Magazine
Store::ID_UKR|CHANNEL 5|Category::1294556|Documentaire
Store::ID_UKR|CHANNEL 5|Category::1299556|Culture
Store::ID_UKR|CHANNEL 5|Category::1326557|Sport

我想 运行 使用命令行 perl cat.pl --name "Store::ID_FRA" 我的脚本 选项是 --name 并且可以是第一列中的任何 Store::ID ,这里我选择 "Store::ID_FRA" 。如果用户未设置任何选项,则脚本不得执行任何操作。

我想输入一个 %getopt (Store::ID 和用户选择的类别串联的结果,以便稍后迭代) 并显示从 Store::ID

附加的每个类别的结果
Store::ID_FRA|Category::1294563
Store::ID_FRA|Category::1294059
Store::ID_FRA|Category::1295062
Store::ID_FRA|Category::1300056
Store::ID_FRA|Category::1299056
Store::ID_FRA|Category::1295060
Store::ID_FRA|Category::1294058
Store::ID_FRA|Category::1294562

到目前为止我所做的是使用键 Store::ID."|".Category 进入 %hash 我的输入文件,对于文件本身的值

然后我创建另一个 %channel 以仅具有 Store::ID 我可能认为可以将这种带有散列的数据放入另一个散列中。不知道怎么处理

这里是脚本

#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use feature 'say';
use Getopt::Long;

my $file = "/home/load/categories_file";
my (%hash,%channel,%getop) = ();

# Script
if (-e $file && ! -z $file) {
    open (my $TOP, "<", $file) or die ("Can't open \"$file\": $!\n");
    while (<$TOP>) {
        chomp;
        my @tab = split(/\|/, $_);
        my ($id,$name,$categories,$cat_name) = ($tab[0],$tab[1],$tab[2],$tab[3]);
        if (! $hash{$id."|".$categories}) {
             $hash{$id."|".$categories} = $id . "|" . $name . "|" . $categories . "|" . $cat_name;
             $channel{$id} = $id;
        }
    }
    close ($TOP);
}


# Getopt
GetOptions( "name:s" => \my $name );
if (defined $name) {
        $getop{$name} = $name;
}
else {
        my %exclude = (
                "null","null"
        );
        foreach my $line (sort keys %channel) {
                if (! $getop{$line}) {
                        $getop{$line} = $line;
                        if ($exclude{$line}) {
                                delete ($getop{$line});
                        }
                }
        }
}

if (%getopt) {
    foreach (sort values %getopt) {
        # do something;
    }
}

__END__

我不太清楚你想要达到什么目的,但以下内容对你有帮助吗:

use feature qw(say);
use strict;
use warnings;
use Getopt::Long;

my $file = "categories_file";
my %hash;
open (my $TOP, "<", $file) or die "Can't open \"$file\": $!\n";
while (<$TOP>) {
    chomp;
    my ($id, $channel, $category, $cat_name) = split /\|/;
    $id =~ s/^\QStore::\E//;
    $category =~ s/^\QCategory::\E//;
    push @{$hash{$id}{categories}}, $category;
    push @{$hash{$id}{channels}}, $channel;
    push @{$hash{$id}{cat_names}}, $cat_name;
}
close ($TOP);

GetOptions( "name:s" => \my $name ) or die "Bad options";
if (exists $hash{$name}) {
    my $categories = $hash{$name}->{categories};
    my $channels = $hash{$name}->{channels};
    my $cat_names = $hash{$name}->{cat_names};
    # do something
}
else {
    say "Name $name not found!";
}