|Perl: the Markov chain saw|
Re^3: Purpose of =~ and = in this statementby Marshall (Canon)
|on May 25, 2022 at 20:06 UTC ( #11144186=note: print w/replies, xml )||Need Help??|
I really enjoyed the posts from haukex - all great advice.
There is another formulation of your "if this" then do "that" task. I see that chapter "numbers" could be a string like "11V". You also don't say, but I suspect that there are other statements like your chapter 5 statement for other chapters.
Instead of this being chapter number 5 specific, you could have a regex something like this at the beginning of the loop which would apply to any chapter string, not just chapter 5:
In both of the statements above, the string "5" is unaffected, but if it has some extra stuff like "5VI", the "VI" is removed. I would put a statement like that the beginning of the loop because it handles all number possibilities.
A subtle point is that a "number" in Perl could be represented by a "string" or an actual "numeric value". By and large, you do not have to worry about this because Perl will "do the right thing". Your statement says: if the string that represents "chapter" contains a "5", then set chapter to a numeric 5 (meaning binary 0000101 instead of whatever the string "5" or "657"codes to).
Perl makes this "string representing a number" to "actual binary number" conversion for you automatically. So, this is fine:
In order to do the numeric comparison, Perl will convert the string "3" to binary and compare that to 000011.
BTW, there is one non-numeric string which Perl will convert to numeric without a warning: "
One place where the difference between string and numeric representations of a number can occur is with leading zeroes.
In some situations, I need an integer binary value anyway to put into some DB and what I receive as textual description of that number may have a leading zero. Instead of using regex, I just add zero that number. bingo, $num +=0; $num now has a binary representation in Perl whether it did or not before and there will not be any leading zeroes if I print it or use it otherwise in a string context.
In Section Seekers of Perl Wisdom