in reply to concatenation 2 values then pushing to a hash_ref
Hello perlnewby and welcome to the monastery
I made some guesses at what I think you're intending to do and am by no means the most-sophisticated coder around here, but I think I'll be able to help you along a bit, as I do have some output that you can look at and compare to your needs. I had to change a few things to get it to run at all, so I'll list the new script:
use strict; use diagnostics; use warnings; use autodie qw(open close); use Data::Dump qw(dump); #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,]+/); # removes '=' or 's' or +',' & '+' to match 1 or more these characters $hash{$ita}[0] = $spa; #what about if there is no $num at position 1 in 1st file? if ( !defined $num ) { $hash{$ita}[1] = $num; # if defined then keep it for check later in position 2 + in array } } close $in; while (<$in1>) { chomp; my ( $ita, $fren, $num1, $num2 ) = split(/[=\s,]+/); #creates col +of numbers $hash{$ita}[1] = $fren; #now hashs format will look like this: ita= +> spa fren #define if there is are numbers in position 3 and 4 concat to p +osition 3 if ( defined($num1) and defined($num2) ) { $hash{$ita}[3] = $num1 . $num2; # concat if numbers defined in poition 1 and 2 and s +tore in 3 } elsif ( defined $num1 ) { $hash{$ita}[3] = $num1; #if array has num in pos 2 then save number in positio +n 3(dieci) } } close $in1; foreach my $ita ( keys %hash ) { if ( $hash{$ita}[0] and $hash{$ita}[1] ) { print $out "$ita =>", join( ',', @{ $hash{$ita} } ), "\n"; #line 5 +4 } else { print $out1 "$ita =>", join( ',', @{ $hash{$ita} } ), "\n"; } } close $out; close $out1;
New data sets of same cardinality:
Test_Data_More_RandNumbers.txt: due =deux, two, tre = trois,drei, attro = quatre,four cinque = cinq,funf, five sei = six , six sette = sept , seven ,sechs dieci =dix undici = onze,eleven tredici = treize,thirteen, dreizehn Test_Data_RandNumbers.txt: uno = uno,eins due = dos,zwei tre = tres,drei quattro = quatro cinque = cinco,funf sei = seis, sette = siete,sechs otto = ocho nouve = nueve, neun
I changed the path of the file to be opened to be in the same as the script. "../" opens the parent, but I don't know what "./" was to open. I added a period where you indicated there was to be concatenation, which seems apropos. I changed the subscript 1 to correspond to the second position, as it usually does. Since I was drawing a warning that I was using an unitialized variable on line 54, I changed the data sets to be equally-long. I marked line 54 for those who might diagnose it better. I uncommented the close statements. Finally, I ran it through perltidy, which I heartily recommend. Here is 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,,two sette =>siete,sept,,sevensechs cinque =>cinco,cinq,,funffive tre =>tres,trois,,dreithree OUT_Test_data_NO_match_SpanFren.txt dieci =>,dix uno =>uno nouve =>nueve otto =>ocho, undici =>,onze,,eleven tredici =>,treize,,thirteendreizehn
Clearly there's some comma issues at a minimum, but there is output, and just having that can help you be specific about what you need to modify. Many happy adventures with perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: concatenation 2 values then pushing to a hash_ref
by perlynewby (Scribe) on Jun 12, 2015 at 07:13 UTC | |
by Aldebaran (Curate) on Jun 12, 2015 at 09:57 UTC | |
by perlynewby (Scribe) on Jun 12, 2015 at 19:09 UTC |