Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Anonymous Monk
on Jun 09, 2015 at 18:41 UTC ( [id://1129689]=note: print w/replies, xml ) Need Help??


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

I have corrected my piece of code with our help.

After this is completed then I will add more numbers/complexity to make a list of list program. will add German and English in random order then check for keys and values.

then moving to hash tables as per earlier suggestion

I understand why I can not use the push function(due to french word in place of Spanish , if no Spanish word found)

Still, I see a PROBLEM with my unless section. I am coming up with an empty matrix. trying to use only one hash...

while(<$in1>){ chomp; my ($ita,$fran)=split /\s*=\s*/; #diventa in due colonne $hash{$ita}[1]=$fran; #salva i numeri francesi al loro posto [1] #se i numeri esiste in ambe matrice if ($hash{$ita}[0] and $hash{$ita}[1]){ print $out "$ita =>",join(',',@{$hash{$ita}}),"\n"; } else { print $out1 "$ita =>", join(',',@{$hash{$ita}}),"\n"; } } close $in1; close $out; close $in1;

Replies are listed 'Best First'.
Re^7: Need advice on checking two hashes values and keys
by aaron_baugher (Curate) on Jun 09, 2015 at 20:52 UTC

    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.

      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.

        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"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found