in reply to Importing data to build an array

It would be interesting to know how the files are ordered. Your post implies that the second file is in the same order as the first, but with gaps, and your response to Perlbotics says that you need to maintain the same order. This code seems to do that with the data you've provided.

use strict; use warnings; use Data::Dumper; open my $firstFH, q{<}, \ <<END1 or die qq{open: < HEREDOC: $!\n}; DISTINGUERE TRA;1;14;507;0,000000242475382686773;0,0000033946553576148 +2;0,000122935019022194;0,00000000041732202096217;9,18246152003019;9,1 +8246152003019 MANCANTE DI;1;56;507;0,000000242475382686773;0,0000135786214304593;0,0 +00122935019022194;0,00000000166928808384868;7,18246152003019;7,182461 +52003019 APPLICARE SU;1;64;507;0,000000242475382686773;0,0000155184244919535;0, +000122935019022194;0,00000000190775781011278;6,9898164420878;6,989816 +4420878 MONTATO IN;1;78;507;0,000000242475382686773;0,0000189130798495683;0,00 +0122935019022194;0,00000000232507983107495;6,70441422322555;6,7044142 +2322555 IMPIEGATO IN;2;180;507;0,000000484950765373545;0,0000436455688836191;0 +,000122935019022194;0,00000000536556884094218;6,49796334575812;12,995 +9266915162 RAGGRUPPARE IN;1;109;507;0,000000242475382686773;0,0000264298167128582 +;0,000122935019022194;0,00000000324915002034832;6,22163211731087;6,22 +163211731087 END1 my @order = (); my @arr1 = (); while( <$firstFH> ) { chomp; my( $col1, $lastVal ) = ( split m{;} )[ 0, -1 ]; push @order, $col1; push @arr1, $lastVal; } close $firstFH or die qq{close: < HEREDOC: $!\n}; open my $secondFH, q{<}, \ <<END2 or die qq{open: < HEREDOC: $!\n}; DISTINGUERE TRA;1;14;507;0,000000242475382686773;0,0000033946553576148 +2;0,000122935019022194;0,00000000041732202096217;9,18246152003019;9,1 +8246152003019 APPLICARE SU;1;64;507;0,000000242475382686773;0,0000155184244919535;0, +000122935019022194;0,00000000190775781011278;6,9898164420878;6,989816 +4420878 MONTATO IN;1;78;507;0,000000242475382686773;0,0000189130798495683;0,00 +0122935019022194;0,00000000232507983107495;6,70441422322555;6,7044142 +2322555 IMPIEGATO IN;2;180;507;0,000000484950765373545;0,0000436455688836191;0 +,000122935019022194;0,00000000536556884094218;6,49796334575812;12,995 +9266915162 END2 my $idx = 0; my @arr2 = (); while( <$secondFH> ) { chomp; my( $col1, $lastVal ) = ( split m{;} )[ 0, -1 ]; if( $col1 eq $order[ $idx ] ) { push @arr2, $lastVal; } else { push( @arr2, q{0} ), $idx ++ until $col1 eq $order[ $idx ]; push @arr2, $lastVal; } $idx ++; } push @arr2, q{0} until scalar @arr1 == scalar @arr2; close $secondFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ @arr1, \ @arr2 ], [ qw{ *arr1 *arr2 } ] ); print q{[} . join( q{; }, @arr1 ) . qq{]\n[} . join( q{; }, @arr2 ) . qq{]\n};

The resultant arrays output using Data::Dumper.

@arr1 = ( '9,18246152003019', '7,18246152003019', '6,9898164420878', '6,70441422322555', '12,9959266915162', '6,22163211731087' ); @arr2 = ( '9,18246152003019', '0', '6,9898164420878', '6,70441422322555', '12,9959266915162', '0' ); [9,18246152003019; 7,18246152003019; 6,9898164420878; 6,70441422322555 +; 12,9959266915162; 6,22163211731087] [9,18246152003019; 0; 6,9898164420878; 6,70441422322555; 12,9959266915 +162; 0]

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Importing data to build an array
by Anonymous Monk on Feb 22, 2009 at 19:45 UTC
    I'll try using your code. Unfortunately the data aren't in the same order. I did the example that way because it was quickier, sorry! I'll see how it works with my data and I'll let you know. Thanks for your help, I really appreciate it!

      Quicker? Not in the long run as it turns out :-)

      Anyway, if the order is different my original code will not work. Try this instead.

      use strict; use warnings; use Data::Dumper; open my $firstFH, q{<}, \ <<END1 or die qq{open: < HEREDOC: $!\n}; DISTINGUERE TRA;1;14;507;0,000000242475382686773;0,0000033946553576148 +2;0,000122935019022194;0,00000000041732202096217;9,18246152003019;9,1 +8246152003019 MANCANTE DI;1;56;507;0,000000242475382686773;0,0000135786214304593;0,0 +00122935019022194;0,00000000166928808384868;7,18246152003019;7,182461 +52003019 APPLICARE SU;1;64;507;0,000000242475382686773;0,0000155184244919535;0, +000122935019022194;0,00000000190775781011278;6,9898164420878;6,989816 +4420878 MONTATO IN;1;78;507;0,000000242475382686773;0,0000189130798495683;0,00 +0122935019022194;0,00000000232507983107495;6,70441422322555;6,7044142 +2322555 IMPIEGATO IN;2;180;507;0,000000484950765373545;0,0000436455688836191;0 +,000122935019022194;0,00000000536556884094218;6,49796334575812;12,995 +9266915162 RAGGRUPPARE IN;1;109;507;0,000000242475382686773;0,0000264298167128582 +;0,000122935019022194;0,00000000324915002034832;6,22163211731087;6,22 +163211731087 END1 my $recCt = 0; my @arr1 = (); my %arr1Order = (); while( <$firstFH> ) { chomp; my( $col1, $lastVal ) = ( split m{;} )[ 0, -1 ]; $arr1Order{ $col1 } = $recCt ++; push @arr1, $lastVal; } close $firstFH or die qq{close: < HEREDOC: $!\n}; open my $secondFH, q{<}, \ <<END2 or die qq{open: < HEREDOC: $!\n}; MONTATO IN;1;78;507;0,000000242475382686773;0,0000189130798495683;0,00 +0122935019022194;0,00000000232507983107495;6,70441422322555;6,7044142 +2322555 APPLICARE SU;1;64;507;0,000000242475382686773;0,0000155184244919535;0, +000122935019022194;0,00000000190775781011278;6,9898164420878;6,989816 +4420878 IMPIEGATO IN;2;180;507;0,000000484950765373545;0,0000436455688836191;0 +,000122935019022194;0,00000000536556884094218;6,49796334575812;12,995 +9266915162 DISTINGUERE TRA;1;14;507;0,000000242475382686773;0,0000033946553576148 +2;0,000122935019022194;0,00000000041732202096217;9,18246152003019;9,1 +8246152003019 END2 my @arr2 = ( 0 ) x scalar @arr1; while( <$secondFH> ) { chomp; my( $col1, $lastVal ) = ( split m{;} )[ 0, -1 ]; $arr2[ $arr1Order{ $col1 } ] = $lastVal; } close $secondFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ @arr1, \ @arr2 ], [ qw{ *arr1 *arr2 } ] ); print q{[} . join( q{; }, @arr1 ) . qq{]\n[} . join( q{; }, @arr2 ) . qq{]\n};

      The output is the same.

      Cheers,

      JohnGG