in reply to Re^2: concatenation 2 values then pushing to a hash_ref
in thread concatenation 2 values then pushing to a hash_ref
As a humble scribe, I welcome the opportunity to assist you in this matter. It's a challenge for me to understand exactly what you intend, and it is for you as well. The exercise of writing and debugging perl helps us to specify our intent, in particular, when informed by the script, input, and output. Having read your response, I've changed all three. I changed the input back to what you posted in the original post. I made several changes to the script; it's best just to list it before commenting too much:
use strict; use diagnostics; use warnings; use autodie qw(open close); use Data::Dumper; use 5.010; #declare variables my %hash; my $data; #opening Files using autodie to cut typing... open my $in, '<', "Test_Data_RandNumbers.txt"; open my $in1, '<', "Test_Data_More_RandNumbers.txt"; open my $out, '>', "OUT_Test_Data_Ita_SpanFren_rest.txt"; open my $out1, '>', "OUT_Test_data_NO_match_SpanFren.txt"; #open my $out2,'>' , "./Test_Data_Out_None_Match.txt"; while (<$in>) { #data manipulation to clean up ='s and ,'s #dieci = diez, zehn -->worse case, remove spaces and = and comma; #quattro = quatro -->only one number with spaces or not in from of = +... chomp; my ( $ita, $spa, $num ) = split(/[=\s,]+/); say "values are $ita $spa $num"; $hash{$ita}[0] = $spa; #what about if there is no $num at position 1 in 1st file? if ( defined $num ) { $hash{$ita}[2] = $num; } } close $in; while (<$in1>) { chomp; my ( $ita, $fren, $num1, $num2 ) = split(/[=\s,]+/); say "values are $ita $fren $num1 $num2"; $hash{$ita}[1] = $fren; #now hash's format will look like this: ita +=> spa fren #define if there are numbers in position 3 and 4 concat to positio +n 3 if ( defined($num1) and defined($num2) ) { $hash{$ita}[3] = "$num1 $num2"; } elsif ( defined $num1 ) { $hash{$ita}[3] = $num1; } } close $in1; foreach my $ita ( keys %hash ) { if ( $hash{$ita}[0] and $hash{$ita}[1] ) { print $out "$ita =>", join( ',', @{ $hash{$ita} } ), "\n"; } else { print $out1 "$ita =>", join( ',', @{ $hash{$ita} } ), "\n"; } } print Dumper(\%hash); close $out; close $out1;
I use the feature "say" to understand what values are going into the script. I get a lot of undefined values, but they don't "sink the ship," as it were. I re-wrote my concatenation syntax to mimic yours--I added a space in the middle of a quoted string--yet I wonder if this is actually what you want as far as the logic is concerned. Let's take a look at the output:
C:\Users\Fred\Desktop\pm>type OUT* OUT_Test_Data_Ita_SpanFren_rest.txt sei =>seis,six,,six quattro =>quatro,quatre,,four due =>dos,deux,zwei,two dieci =>diez,dix,zehn sette =>siete,sept,sechs,seven sechs cinque =>cinco,cinq,funf,funf five undici =>once,onze,elf,eleven tre =>tres,trois,drei,drei three tredici =>trece,treize,dreizehn,thirteen dreizehn OUT_Test_data_NO_match_SpanFren.txt dodici =>doce uno =>uno,,eins nouve =>nueve,,neun otto =>ocho
More informative might be the output from STDOUT:
$VAR1 = { 'sei' => [ 'seis', 'six', '', 'six' ], 'quattro' => [ 'quatro', 'quatre', undef, 'four' ], 'due' => [ 'dos', 'deux', 'zwei', 'two ' ], 'dieci' => [ 'diez', 'dix', 'zehn' ], 'sette' => [ 'siete', 'sept', 'sechs', 'seven sechs' ], 'dodici' => [ 'doce' ], 'uno' => [ 'uno', undef, 'eins' ], 'nouve' => [ 'nueve', undef, 'neun' ], 'cinque' => [ 'cinco', 'cinq', 'funf', 'funf five' ], 'otto' => [ 'ocho' ], 'undici' => [ 'once', 'onze', 'elf', 'eleven' ], 'tre' => [ 'tres', 'trois', 'drei', 'drei three' ], 'tredici' => [ 'trece', 'treize', 'dreizehn', 'thirteen dreizehn' ] };
I ask you to inspect the output. Aside from the commas, is this what you want? It seems to me that the commas is a cosmetic issue, while determining the fundamental logic is primary. What's more, I don't think you want concatenation, as it produces duplicate values. But, as a humble scribe, I have no means to scry.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: concatenation 2 values then pushing to a hash_ref
by perlynewby (Scribe) on Jun 12, 2015 at 19:09 UTC |