1

I'm trying to check the existence of words from a file in a hash, and if so to display the corresponding hash key and value.

So far, I've been able to do it with an array @motcherches which values are given in the script.

I can't find a way to give as values to this array words from a external file (here FILEDEUX). Could you point me in the right direction?

(Please don't hesitate to correct my way of explaining my problem.)

#!/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;

EDIT: Example inputs

FILE (transformed into %elements hash)

Zieler player
Chilwell player
Ulloa player
Mahrez player
Ranieri coach

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

FILEDEUX (transformed into motcherchesarray)

One save is quickly followed by another, and this time Zieler had to be a little sharper to keep it out. Izaguirre gives Mahrez a taste of his own medicine by beating him all ends up down the left flank, although the Leicester winger gave up far too easily. Claudio Ranieri will be happy with what he has seen from his side so far.

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

Expected output:

Zieler player
Mahrez player
Ranieri coach
2
  • 1
    use warnings is similar to -w on your shebang line. You only need one of them (and most people would go with the use warnings). Commented Jul 29, 2016 at 11:04
  • Missed that, thanks! Commented Jul 29, 2016 at 11:29

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

@Baptiste - Great. I'm going to keep the answer as is (rejecting your edit suggestion), just because using __DATA__ to read in data within a script is so common
1

Everything is fine but add the chomp in your program

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

2 Comments

I'm not sure you understood my problem right: what I'm struggling with is the my @motcherches = qw/blop blup blip blap/; line : it works great as it is, but I'd like not to have "blop blup blip blap" as values but instead words from a file, as I did with the other file. What I've tried up until now returned errors.
@Baptiste Please add example input .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.