Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Not printing the values outside the while loop

by suvendra123
on Jun 09, 2021 at 16:17 UTC ( #11133691=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Not printing the values outside the while loop
by AnomalousMonk (Bishop) on Jun 09, 2021 at 20:12 UTC
    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:


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

Re: Not printing the values outside the while loop
by LanX (Sage) on Jun 09, 2021 at 16:24 UTC
    Please show us

    • input (test.txt)
    • actual output
    • expected output

    in an SSCCE

    Most probably is the last line just empty.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Not printing the values outside the while loop
by cavac (Curate) on Jun 10, 2021 at 10:56 UTC

    Just my thoughts on your post:

    1. use strict;
    2. use warnings;
    3. use Carp; and then call croak() instead of die(). Makes it easier to debug as soon as the code moves into a sub.
    4. one letter variable names are not a good idea, except in things like iterator counters, e.g. for(my $i = 0; $i < 10; $i++)
    5. $a and $b are reserved variable names, used by sort(). Do not use them for anything else.
    6. Unless your variables end in a line break, the print statements wont show up immediately on the command line, because buffered IO.
    7. When i do file operations, i usually chomp() every line, then add linebreaks explicitly to the output when required. Makes it easier to understand the code.
    8. Not sure why you are opening $ofh51 for writing. You don't write to it.
    9. Both the example text file and the example output should be in their own code tags for easier reading as well as downloading.
    10. Personally, i also prefer singular/plural naming of variables. So, for example it would be @results. When accessing an element of that, it would be $results[1], because its "one of many". On the other hand, an iterator would be singular, something like foreach my $result (@results). But that's just me...

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
Re: Not printing the values outside the while loop
by GrandFather (Saint) on Jun 11, 2021 at 03:36 UTC

    Each time through the loop you replace the contents of all the variables regardless if matching the relevant text. For input lines that contain less than all the search strings at least some of the variables will be empty.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-09-24 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?