【发布时间】:2022-01-11 15:13:08
【问题描述】:
我有一个输入这个的文件
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__
【问题讨论】:
-
我希望输出带有输入文件类别的 Store::ID,该类别已被 (the Store::ID) 设置为脚本
标签: perl getopt-long