Fshah has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,

I have an array which is joined by ';' In the next loop I get empty values I want to find the empty values and replace them by the one's obtained in the previous loop

@first_loop=(1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720; +1;9;25;0;2; Carry 0); @next_loop=(1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1);

I want the output in the present array to not have empty values instead have previous value

thank you for the help in advance edit: I have an array of such data in each line I want to perform this operation on entire array
1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720;1;9;25;0;2; C +arry 0 1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1 1671;1661;;0;;;0;0;0;0;;;;;;1;9;;0;2; Carry 2

Replies are listed 'Best First'.
Re: How to replace empty values with value in previous array
by huck (Prior) on Jul 20, 2017 at 07:08 UTC

    I have an array which is joined by ';'

    you CANT "have an array which is joined by ';'". you could have a string where ';' is a delimiter. So your example code wont even work. did you even try it first?

    Anyway if they were valid arrays

    for my $i (0..$#next_loop) { $next_loop[$i]=$first_loop[$i] unless (defined ($next_loop[$i]) and +$next_loop[$i] ne ''); }

Re: How to replace empty values with value in previous array
by AnomalousMonk (Archbishop) on Jul 20, 2017 at 07:18 UTC

    As huck pointed out, you seem to be starting with CSV-like strings. If that's the case, do yourself an enormous favor and use Text::CSV_XS. One way:

    c:\@Work\Perl\monks\Fshah>perl -wMstrict -le "use Text::CSV_XS; use Data::Dump qw(dd); ;; my $first_record = '1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720;1;9;25;0; +2; Carry 0'; my $second_record = '1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1'; ;; my $csv = Text::CSV_XS->new({ sep_char => ';' }) or die 'Text::CSV_XS->new{}: ', Text::CSV->error_diag(); ;; $csv->parse($first_record) or die '1st parse failed'; my @first_fields = $csv->fields; dd \@first_fields; ;; $csv->parse($second_record) or die '2nd parse failed'; my @second_fields = $csv->fields; dd \@second_fields; ;; die 'number of fields differ' unless @first_fields == @second_fields; ;; @second_fields = map { length($second_fields[$_]) != 0 ? $second_fields[$_] : $first_fields [$_] } 0 .. $#first_fields ; dd \@second_fields; " [ 1668, 1733, 60, 32, 3173, 0, 2517, 58221, 55764, 0, 0, 0, 0, "Td", 30720, 1, 9, 25, 0, 2, " Carry 0", ] [ 1671, 1661, 0, 0, "", "", 0, 0, 0, 0, "", "", "", "", "", 1, 9, 25, 0, 2, " Carry 1", ] [ 1671, 1661, 0, 0, 3173, 0, 0, 0, 0, 0, 0, 0, 0, "Td", 30720, 1, 9, 25, 0, 2, " Carry 1", ]


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

Re: How to replace empty values with value in previous array
by poj (Abbot) on Jul 20, 2017 at 07:58 UTC
    #!perl use strict; my @prev = (); my @data = (); while (<DATA>){ chomp; my @row = split ';',$_; for my $i (0..$#row){ if ( length ($row[$i]) == 0 ){ $row[$i] = $prev[$i]; } } push @data,\@row; @prev = @row; } for (@data){ print join "\t",@$_,"\n"; } __DATA__ 1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720;1;9;25;0;2; C +arry 0; 1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1;
    poj

      Thanks for the reply, I have an array with 100's of these lines how can I perform the same operation over entire array line by line

        Do you want to create a new array or overwrite the existing ?

Re: How to replace empty values with value in previous array
by BillKSmith (Monsignor) on Jul 20, 2017 at 16:32 UTC
    It is probably easier to make this correction while you are creating the 'arrays' than it is to fix them later. Is this an option?
    Bill