[英]Perl compare array content to hash values
我正在嘗試檢查哈希文件中是否存在單詞,如果顯示,則顯示相應的哈希鍵和值。
到目前為止,我已經能夠使用@motcherches
數組來完成此操作,該值在腳本中給出。
我找不到從外部文件(此處為FILEDEUX
)為該數組單詞提供值的方法。 你能指出我正確的方向嗎?
(請不要猶豫,改正我解釋問題的方式。)
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
my $hashtable = $ARGV[0];
my $textecompare = $ARGV[1];
open FILE,"<$hashtable" or die "Could not open $hashtable: $!\n";
open FILEDEUX,"<$textecompare" or die "Could not open $textecompare: $!\n";
while ( my $line = <FILE>)
{
chomp($line);
my %elements = split(" ",$line);
my @motcherches = qw/blop blup blip blap/;
foreach my $motcherches (@motcherches) {
while ((my $key, my $value) = each (%elements)) {
# Check if element is in hash :
say "$key , $value" if (exists $elements{$motcherches}) ;
}
}
}
close FILE;
close FILEDEUX;
編輯:示例輸入
FILE
(轉換為%elements
哈希)
Zieler player
Chilwell player
Ulloa player
Mahrez player
Ranieri coach
================================================== ==========================
FILEDEUX
(轉換為motcherches
數組)
一次保存之后很快就保存了另一遍,這一次Zieler必須更加犀利才能將其保留。 伊薩吉爾(Izaguirre)擊敗馬赫雷斯(Mahrez),讓他嘗嘗自己的良葯,盡管萊斯特邊鋒的放棄太容易了,但他最終都落在了左翼。 克勞迪奧·拉涅里(Claudio Ranieri)將對他迄今為止所見所聞感到滿意。
================================================== ===========================
預期產量:
Zieler player
Mahrez player
Ranieri coach
一切都很好,但是在程序中添加了chomp
while ( my $line = <FILE>)
{
chomp($line);
my %elements = split(" ",$line);
use strict;
use warnings;
use feature qw/ say /;
# Read in your paragraph of text here:
open my $text, '<', 'in.txt' or die $!;
my %data;
while(<$text>){
chomp;
$data{$_}++ for split; # by default split splits on ' '
}
my %players;
while(<DATA>){
chomp;
my @field = split;
say if $data{$field[0]};
}
__DATA__
Zieler player
Chilwell player
Ulloa player
Mahrez player
Ranieri coach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.