Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Not printing the values outside the while loop

by AnomalousMonk (Archbishop)
on Jun 09, 2021 at 20:12 UTC ( [id://11133702]=note: print w/replies, xml ) Need Help??


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:

  • @result = grep (/bist_mode/, $line51);     grep is operating on a list consisting of a single scalar, $line51, so @result can have only zero (i.e., no match) or one element.
  • $b = join("_",@result);     join can only operate effectively on lists of two or more elements. @result can only have 0 or 1 element, so the output of join is either the empty string (0 elements) or the input string (1 element).
  • print $b;     This will print the processed line if there was a match, or the empty string if there was no match. Outside the loop, the result for the most recently processed line will be printed, i.e., the empty string if there was no match on the last input line; see LanX's final comment on this here.

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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-25 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found