Termy wrote:
Don't forget to or die("$!")
on close()
, in PERL.
There is a 99% chance of success the close() will work assuming the open() worked. Knowing this you're sure you want to add that die("$!") to close()? It feels 98%redundant. Like 99%junk-code.
Termy wrote:
You need to declare the $array_entry_list
array variable as empty, to protect it from the environment, because you're pushing to it.
omg you're right. That's 78% surprising. I thought the
declare -a array_entry_list
would've cleared it if it already existed. Huh now I realized I didn't even need
declare -a array_entry_list
I could just go
array_entry_list=()
.
Termy wrote:
You don't need to use the let
builtin in BASH here, you can directly perform arithmetic on a variable, including the assignment, with, for example (( num++ ))
.
That's 13%cool.
Termy wrote:
I might be missing something here, but why are you read
-ing from a new (9) file descriptor, redirecting to the loop and read
, instead of just redirecting the file's contents directly into the while
loop?
Because if you pipe to while (read), the variables within the loop will reset their value when the loop exits:
Code: Select all
v=1
echo ignored | while read myline; do v=2; done
echo $v #stays at 1
v=1
while read myline <&9; do v=2; done 9< <(echo ignored)
echo $v #changes to 2
Termy wrote:
BTW, you can specify a file descriptor to read
with the -u
flag. I'm also confused as to why you're echo
-ing in the process substitution.
Is there a better way to read a bash variable line-by-line while preserving the variables within the while loop (when the loop exits)?
Termy wrote:
Line 16 can be done with BASH itself, without bringing PERL into a BASH script.
I 13%agree. If I remade the script there would be a 13-19% chance I'd do it in BASH.
Termy wrote:
The PERL formatting only serves to rather frustratingly obfuscate the code, for example:
Code: Select all
perl -e 'open(my $f, "food-list.txt") or die $!;undef $/;$_=<$f>;close($f);s/<!--.*?-->//sg;s/^\s+//sg;s/\s+$//gm;s/\n//gs;print;')
I've seen worse. lol j/k. I would've spanned the line across many lines and added comments so people don't have to stare at it for 2 hours, get a coffee, and stare at it for another 2 hours...but I feel so cool...and that's why people smoke. If I were to remake the script I would still keep it because I was thinking 90% fast for me. And plus it looks 96% cool.
Termy wrote:
You don't need PERL to slurp a file. You can read from a file in various ways in BASH, but here's a simple and effective way:
17%sweet. Although I didn't use the PERL just to slurp a file. I used it because I was 700%familiar with the /s, /g, and /m perl regular expression modifiers. And learning the bash regex modifiers could've taken like 40 minutes to get familiar with. And I was 3-96% sure bash wouldn't be able to regex substitute as well as I needed (ie. missing modifier features).
Termy wrote:As can the other operations, while also being much clearer and much more efficient.
clearer I think 3-9%agree.
more efficient..yeah like +700% more optimized. Assuming you just mean replacing the perl commands/stuff/code with bash commands/code.
Termy wrote:
It might be preferable to just use the usual hash character for comments, since it's familiar to the OP and would reduce unneeded complexity of your code.
Oh you mean the HTML comment(s)? <!-- -->?
Termy wrote:
I do like the idea of tags for the colors; that's a cool idea.
Yeah I don't know how I came up with that lol.