Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^7: Need advice on checking two hashes values and keys

by aaron_baugher (Curate)
on Jun 09, 2015 at 20:52 UTC ( #1129706=note: print w/replies, xml ) Need Help??


in reply to Re^6: Need advice on checking two hashes values and keys
in thread Need advice on checking two hashes values and keys

I'm not sure I understand your problem; you might need to show what the output looks like. But if this is your actual code, one problem is that your if/else statement which does the printing is inside your while() loop that adds the French values, so it's going to print the whole thing every time it processes a line from your Italian-French file. That if/else section should be after the while loop, in its own for loop, as I showed in my last post to which you replied.

In other words, you'll have three independent loops:

  • A while loop to process the first file
  • A while loop to process the second file
  • A for loop to go through the hash and print the keys and values

None of these should be within the others.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

  • Comment on Re^7: Need advice on checking two hashes values and keys

Replies are listed 'Best First'.
Re^8: Need advice on checking two hashes values and keys
by Anonymous Monk on Jun 10, 2015 at 00:15 UTC

    I see the problem with doing the check within the while loop.

    I wanted to only use one hash but I see I need more than one.

    ok, let me give it a shot as per your suggestion

    totally a different question, I have modify my NEXT test program to include other numbers and I need your expert help with regexp...can you help me construct the regexp? I have a question on a word that maybe or not be in the string and be separated by a comma

    for example:

    dieci = diez, zehn quattro = quatro sei = seis, sette = siete,sechs
    while ($in){ #data manipulation to clean up ='s and ,'s chomp; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;
      $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

      First thing: if you don't supply a variable, the regex will automatically work on $_, so you shouldn't include that. It's confusing and may lead to bugs. Just put the /regex/.

      Second, your regex doesn't actually do anything. It checks the pattern against your chomped line, but it doesn't do anything to it. To manipulate it, you'd need to add another section and make it a s/// operator, with the replacement portion between the second and third delimiters. I don't know what you do want to do to it, so for now, here's what your pattern is checking for. I'm going to use the /x modifier so that I can include whitespace and comments, which I'd suggest you do in the future unless you're dealing with a simple enough regex that it's obvious at a glance what it does.

      /^ # anchored to the beginning of the string \w+ # one or more word characters (a word) \s* # zero or more whitespace characters = # an equals sign \s* # zero or more whitespace characters \w+ # one or more word characters (a word) \s* # zero or more whitespace characters \, # a comma (you actually don't need to backslash this) \? # a question mark \w+ # one or more word characters (a word) /x;

      As you can see now, this will require that a matching line include a comma followed by a question mark. That's probably not what you want. It also requires a third "word" following the question mark. A good method would be to write out exactly what you want to match, as I've done with my comments above, and then figure out the regex pattern piece by piece from there. Then explain what changes you want to make to it, and we can figure out the second part.

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

        $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

        Actually, it's worse than that. Because the  = ~ is screwed up (should have been =~), the statement (if that's the statement that's actually in perlynewby's code (if that's actually perlynewby)) is assigning the 1s-complement of the numeric value of the result of the  m// against  $_ to  $_ instead:

        c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'x=x,?x'; print qq{'$_'}; ;; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ; print qq{'$_'}; ;; $_ = '----'; print qq{'$_'}; ;; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ; print qq{'$_'}; " 'x=x,?x' '4294967294' '----' '4294967295'


        Give a man a fish:  <%-(-(-(-<

      ok, now, I am stuck here. I spend time testing/reading on split with various delimiters.I learned how to remove stuff I don't want...

      BUT

      now I get some error I DON"T understand after 1.5 weeks of playing with perl. I'm so FRUSTRATED with my prgress!

      can you help me out by explaining my error. I am too inexperience to SEE it myself.

      first data key has random values of 2 or less numbers

      uno = uno,eins due = dos,zwei tre = tres,drei quattro = quatro cinque = cinco,funf sei = seis, sette = siete,sechs otto = ocho nouve = nueve, neun dieci = diez, zehn undici = once, elf dodici = doce tredici = trece, dreizehn

      2nd data may have 3 or less vaules

      due =deux, two, tre = trois,drei, three quattro = quatre,four cinque = cinq,funf, five sei = six , six sette = sept , seven ,sechs dieci =dix undici = onze,eleven tredici = treize,thirteen, dreizehn
      use strict; use warnings; #declare variables my %hash; my $data; #opening Files open my $in, '<',"./Test_Data_RanNumbers.txt" or die ("can't open the +file:$!\n"); open my $in1,'<',"./Test_Data_More_RanNumbers.txt" or die ("can't open + file : $!\n"); open my $out ,'>' ,"./Test_Data_Out_Ita_SpanFrenEngGer.txt" or die "ca +n't open the file for write:$!\n"; open my $out1,'>',"./Test_data_out_Ita_SpanFren.txt" or die "can't ope +n the file :$!\n"; open my $out2,'>' , "./Test_Data_Out_None_Match.txt" or die "can't ope +n file for write:$!\n"; while ($in){ #data manipulation to clean up ='s and ,'s #dieci = diez, zehn -->worse case, remove spaces and = and comm +a; #quattro = quatro -->only one number with spaces or not in from of + =... chomp($data); my ($ita,$spa,$num)= split(/[=\s,]+/,$data); # removes '=' or 's' +or ',' & '+' to match 1 or more these characters $hash{$ita}[0]=$spa; $hash{$ita}[1]=$num; #keeping it for check later if "defined" | wi +ll be undef if not } close $in; while ($in1){ chomp($data); my ($ita,$fren,$num)= split(/[=\s,]+/,$data); $hash{$ita}[0]=$fren; $hash{$ita}[1]=$num; #keeping for check later checks programming } close $in1; foreach my $ita (keys %hash){ if($hash{$ita}[0] and $hash{$ita}[1]){ print "$ita =>", join(',',@{$hash{$ita}}),"\n"; }else { print "$ita =>",join(',',@{$hash{$ita}}),"\n"; } }

        never mind...forget about it ;-)

        I am back at it again with clear mind and eyes and I see what I need to do...I think my frustration disadvantage the best of me.

        I still like Perl ;-) until next bug in my code...but practicing

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1129706]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2020-05-27 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (162 votes). Check out past polls.

    Notices?