Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I agree with the earlier responses that a regex is a better solution that split here. However, I noticed that all the suggested regex solutions access $1 et al. without making sure the regex match succeeded. A failed match does not reset the special regex variables -- they keep their values from the previous successful match!

Here's one way to check the success of the match. Of course, you can change the structure, but the basic idea is to only access $1 et al. if the match succeeds.

my $inLine = "RPC, rpc #001b, (1987)"; my $year; if ($inLine =~ /\((\d+)\)/) { $year = $1; } else { die "No year found in '$inLine'.\n"; } print "Found year '$year' in '$inLine'.\n";

 

Here's a demonstration of a bug caused by not checking the success of the match.
my $inLine = "RPC, rpc #001b, 1987"; $inLine =~ /(#\d+[a-z])/; my $rpcNum = $1; print "Found rpc num '$rpcNum' in '$inLine'.\n"; $inLine =~ /\((\d+)\)/; my $year = $1; print "Found year '$year' in '$inLine'.\n";
This produces the following output.
Found rpc num #001b in RPC, rpc #001b, 1987. Found year #001b in RPC, rpc #001b, 1987.

In reply to Re: split question by chipmunk
in thread split question by Anonymous Monk

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, 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, 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
    Chatterbox?
    [MidLifeXis]: heh.
    [MidLifeXis]: Most likely it is a code that some undocumented system, hidden behind layers of IT, deep in the bowels of the building under the machine room floor, reads that code to keep a presence switch from going off. :-b
    [MidLifeXis]: I think I forgot "running on a farm of commodore 64, vic 20s, trs 80s, and apple ]|[e systems"
    [GotToBTru]: oh I know what it is .. but it is a number only slightly useful to me and of no possible use to our customer
    [MidLifeXis]: Whew - you just saved the free world. <o)
    [GotToBTru]: i guess it's a placeholder, the code will only fill it in if there is nothing else to use
    [GotToBTru]: but then .. if you have nothing to say, why not say nothing?

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (9)
    As of 2017-01-20 19:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you watch meteor showers?




      Results (176 votes). Check out past polls.