Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

$chapter =~ s/\D+//g; # remove all non-digit characters # or perhaps to avoid the /g flag, (I wouldn't code it this way # because it is overly complex) however: $chapter =~ s/^(\D*)(\d+)(\D*)/$2/; # remove all optional non-digit s +tuff # before or after the digits # try the above with "XX546YYY", just "453ZZ" and "AAA123ZZZ77548" as # cases to probe the limits... what happens if it is not just "11VI"?
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:

my $x = "3"; if ($x > 3){...}
In order to do the numeric comparison, Perl will convert the string "3" to binary and compare that to 000011.

my $x = "chapter 5"; print "chap 5 ok!" if $x == 5; # Throws Warning: Argument "chapter 5" isn't numeric in numeric eq (== +) $x =~ s/\D+//g; # eliminate all non 0-9 characters from string print "chap 5 ok!" if $x == 5; # chapter 5 is ok now! # The string got "fixed" to be completely numeric # Then when Perl made it into binary number to compare against 5, it w +orked!
BTW, there is one non-numeric string which Perl will convert to numeric without a warning: "zero0 but true". That will equal numeric binary zero, but will test as "true" in logical expression. The modern way to do that is the string "0E0". The DBI can use this in some situations where it wants to say: 1)the command worked (true), but 2) there were no valid results returned, i.e. numeric zero. Pretty Cute!

One place where the difference between string and numeric representations of a number can occur is with leading zeroes.

$x = "00005"; print "$x\n"; #yields "00005" $x += 0; #adding zero forces numeric conversion print "$x\n"; #yields "5"
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 reply to Re^3: Purpose of =~ and = in this statement by Marshall
in thread Purpose of =~ and = in this statement by sherab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2025-06-19 07:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.