2016-07-29 58 views
1

我試圖從哈希文件中檢查單詞的存在性,並且如果是,則顯示相應的哈希鍵和值。Perl將數組內容與哈希值進行比較

到目前爲止,我已經可以通過這個數組來完成這個值,這個值在腳本中給出。

我無法找到一種方法來從外部文件(此處爲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必須是清晰一點,以保持它。 雖然萊切斯特的邊鋒很容易就放棄了,但伊扎格雷給了馬赫雷斯一個嘗試,他擊敗了他的左翼,儘管萊斯特邊鋒放棄了。克勞迪奧拉涅利將會很滿意他迄今在他身邊所看到的。

============================================== ===============================

預期輸出:

Zieler player 
Mahrez player 
Ranieri coach 
+1

'使用warnings'類似於'-w'你的家當線。你只需要其中的一個(並且大多數人會使用「使用警告」)。 –

+0

錯過了,謝謝! – Azaghal

回答

1
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 
+0

由於它的偉大工程! – Azaghal

+1

@Baptiste - 偉大的。我要保持答案是(拒絕你的編輯建議),只是因爲使用'__DATA__'在腳本中讀入數據是如此普遍 – fugu

1

一切都很好,但加在程序中chomp

while (my $line = <FILE>) 
    { 
    chomp($line); 
    my %elements = split(" ",$line); 
+0

我不知道你明白我的問題吧:我正在掙扎是 '我@motcherches = QW/blop BLUP曇花一現BLAP /;'行: 它的偉大工程,因爲它是,但我想不要有「blop BLUP曇花一現BLAP」從一個文件中的值,而是的話,因爲我與其他文件一樣。我到現在爲止嘗試過的錯誤。 – Azaghal

+0

@Baptiste請添加示例輸入。 – mkHun

+0

我編輯了我原來的帖子。 – Azaghal

相關問題