in reply to Not printing the values outside the while loop
while(my $line51 = <$ifh51>) { # my $line51 = $_ if /\bbist_mode\b/; @result = grep (/bist_mode/, $line51); @result1 = grep (/mode_sel/, $line51); @result2 = grep (/mem_type_sel/, $line51); $b = join("_",@result); $b =~ s/,([^,]*$)/$1/; $c = join("_",@result1); $c =~ s/,([^,]*$)/$1/; $d = join("_",@result2); $d =~ s/,([^,]*$)/$1/; print $b; print $c; print $d; }
Some general comments on this code:
Consider:
Win8 Strawberry 5.8.9.5 (32) Wed 06/09/2021 15:18:22 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); # my $line51 = 'foo'; for my $line51 ( '', "\n", 'foo', 'foo, bist_mode, bar', 'foo, bist_mode bar', ) { dd '$line51', $line51; my @result = grep (/bist_mode/, $line51); dd 'A', \@result; my $b = join("_",@result); dd 'B', $b; $b =~ s/,([^,]*$)/$1/; dd 'C', $b; print "\n"; } ^Z ("\$line51", "") ("A", []) ("B", "") ("C", "") ("\$line51", "\n") ("A", []) ("B", "") ("C", "") ("\$line51", "foo") ("A", []) ("B", "") ("C", "") ("\$line51", "foo, bist_mode, bar") ("A", ["foo, bist_mode, bar"]) ("B", "foo, bist_mode, bar") ("C", "foo, bist_mode bar") ("\$line51", "foo, bist_mode bar") ("A", ["foo, bist_mode bar"]) ("B", "foo, bist_mode bar") ("C", "foo bist_mode bar")
What I would consider cleaner code for doing the same thing might be something like:
Win8 Strawberry 5.8.9.5 (32) Wed 06/09/2021 15:25:55 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); my $data = <<'EOD'; bist_ctlr_clk_bistctlr, bist_ctlr_rst_bistctlr_n, bist_ctlr_serial_in_system, bist_ctlr_serial_out_system, bist_ctlr_shift_en_system, bist_ctlr_global_bbad, bist_ctlr_global_repairable, bist_ctlr_bist_mode, bist_ctlr_mem_type_sel, bist_ctlr_mode_sel, bist_ctlr_memsafe, bist_ctlr_sif_reg_en_system, bist_ctlr_sif_load_en_system, bist_ctlr_sif_update_en_system, bist_ctlr_mem_atpg_mode, bist_ctlr_flash_data_select, bist_ctlr_flash_repair_data, bist_ctlr_bend, EOD open (my $ifh51, '<', \$data) or die $!; my $rx_match = qr{ bist_mode | mode_sel | mem_type_sel }xms; my @matched; LINE51: while (my $line51 = <$ifh51>) { # no further processing unless something matches. next LINE51 unless $line51 =~ $rx_match; # remove rightmost comma. $line51 =~ s{ , (?= [^,]* \Z) }{}xms; # save what was matched and processed. push @matched, $line51; } dd \@matched; ^Z [ "bist_ctlr_bist_mode\n", "bist_ctlr_mem_type_sel\n", "bist_ctlr_mode_sel\n", ]
Give a man a fish: <%-{-{-{-<
|
|---|