我有一個輸入這個的檔案
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
到目前為止我所做的是%hash使用鍵Store::ID."|".Category和值進入我的輸入檔案本身
然后我創建另一個%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__
uj5u.com熱心網友回復:
我并不完全清楚您想要實作什么,但以下內容對您有幫助:
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!";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409716.html
標籤:
上一篇:嘗試匹配電子郵件中的名稱
