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: <%-{-{-{-<
| [reply] [d/l] [select] |
Just my thoughts on your post:
- use strict;
- use warnings;
- use Carp; and then call croak() instead of die(). Makes it easier to debug as soon as the code moves into a sub.
- one letter variable names are not a good idea, except in things like iterator counters, e.g. for(my $i = 0; $i < 10; $i++)
- $a and $b are reserved variable names, used by sort(). Do not use them for anything else.
- Unless your variables end in a line break, the print statements wont show up immediately on the command line, because buffered IO.
- 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.
- Not sure why you are opening $ofh51 for writing. You don't write to it.
- Both the example text file and the example output should be in their own code tags for easier reading as well as downloading.
- 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";'
| [reply] [d/l] [select] |
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
| [reply] |