1

i want to read a file1 that containfor every word a numeric reprsentation, for example:

clinton 279
capital 553
fond|fonds 1410

I read a second file, every time i find a number i replace it with the corresponding word. above an example of second file

279 695 696 152 - 574
553 95 74 96 - 444
1410 74 95 96 - 447

The problem in my code is that it execute the subroutine only one time. and it only show:

279 clinton

normally in this example it should show 3 words, when i add print $b; in the subrtoutine it show the different numbers.

#!/usr/bin/perl
use stricrt;
use warnings;
use autodie;

my @a,my $i,my $k;
my $j;

my $fich_in = "C:\\charm\\ats\\4.con";
my $fich_nom = "C:\\charm\\ats\\ats_dict.txt";


open(FICH1, "<$fich_in")|| die "Problème d'ouverture : $!";
open my $fh, '<', $fich_nom;

#here i put the file into a table

while (<FICH1>) {
my $ligne=$_;
chomp $ligne; 


my @numb=split(/-/,$ligne);
my $b=$numb[0];
    $k=$#uniq+1;
    print   "$b\n";
     my_handle($fh,$b);
}

sub my_handle {
    my ($handle,$b) = @_;
    my $content = '';
#print "$b\n";

    ## or line wise
    while (my $line = <$handle>){
    my @liste2=split(/\s/,$line);
if($liste2[1]==$b){
        $i=$liste2[0];
            print "$b $i";}
        }
return $i;
}


    close $fh;
    close(FIC1);
5
  • 3
    It looks like you have assembled this program by copy/pasting from various sources. Do you understand what's going on in there? Please edit the question and indent your code properly. Also, please always use strict and use warnings in code that you post on SO (and also when you try to run it on your machine, obviously). Commented Jan 8, 2016 at 12:48
  • this is my code and the problem is it when i pass the paramter $b in the subroutine my_handle($fh,$b);, it only work one time so the subroutine show one word and not all the word that correspond to the paramter $b Commented Jan 8, 2016 at 12:55
  • 2
    Here's why I think you did not completely write this: you don't have strict or warnings, but you have autodie. Still, you have die on one of your opens. You are using a lexical filehandle with 3-argument-open for one file, and a glob and 2-arg-open for the other, and one close has parens while the other does not. I think you added some of the glue in between yourself, because you have @a[$i], which strict would have complained about as the sigil is wrong. It should be $a[$i] because what is inside of the array @a in position $i is scalar, not an array. ... Commented Jan 8, 2016 at 13:01
  • 4
    ... Also $b is reserved and should not be used as it is intended for use inside of a sort block. However, we can try to solve your question. But first, please indent it properly. You can edit the question. Then explain how it is supposed to work, as this code is not doing what you describe in the question. Also, please add at least one more line for the lookup file, and an example of the second input file. Commented Jan 8, 2016 at 13:03
  • @simbabque: i edited the question :) Commented Jan 8, 2016 at 13:50

1 Answer 1

3

The common approach to similar problems is to hash the "dictionary" first, than iterate over the second file and search for replacements in the hash table:

#!/usr/bin/perl
use warnings;
use strict;

my $fich_in = '4.con';
my $fich_nom = 'ats_dict.txt';

open my $F1, '<', $fich_in  or die "Problème d'ouverture $fich_in : $!";
open my $F2, '<', $fich_nom or die "Problème d'ouverture $fich_nom : $!";;

my %to_word;
while (<$F1>) {
    my ($word, $code) = split;
    $to_word{$code} = $word;
}

while (<$F2>) {
    my ($number_string, $final_num) = split / - /;
    my @words = split ' ', $number_string;
    $words[0] = $to_word{ $words[0] } || $words[0];
    print "@words - $final_num";
}
Sign up to request clarification or add additional context in comments.

4 Comments

this code work well, just reverse F1 and F2, and the code has warning use of unintialized value $word[0] in hash element
@ChediBechikh: It's just a warning. Probably coming from empty lines in the file?
you are right, can you epain to me why i find problem with my code, only the first word is terated
@ChediBechikh: I don't understand your code. Maybe you exhaust $fh in the first run and there's nothing else to read from the file?

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.