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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
NOT a general solution, unless all cases closely match the sample presented (which doesn't seem to show any reasonable use of a regex to solve the problem. For a prime example, why would you do the unnecessary work of dividing the word "chapter" into a literal fragment and a captured [a-z]* class? This way lies madness!

OTOH, many will think this plodding, literal-minded code mad.... or at best, inelegant. So be it... in the name of a clear and explicit demo of an algorithm implemented more gracefully by others, above:

#!/usr/bin/perl use 5.016; use Data::Dumper; # 1030798 my $str = "chapter 1,2,3"; # out: "chapter 1, chapter 2, chapter +3" my @arr = split / /,$str; # split to 2 element array, "chapter" & + "1,2,3" my @chaptnums = split /,/,$arr[1]; for my $elem(@chaptnums) { print "$arr[0] $elem"; # e.g. "chapter " . num if (scalar $elem != (1+$#chaptnums) ) { # not the last @chaptnums + element? print ", "; } else { print "\n"; } } =head EXECUTION C:>1030798.pl chapter 1, chapter 2, chapter 3 =cut

Again, CAUTION: The code above is NOT how to do the job; it's for instruction purposes, only.


If you didn't program your executable by toggling in binary, it wasn't really programming!


In reply to Re: how to fetch 1,2,3, from chapter by ww
in thread how to fetch 1,2,3, from chapter by virudinesh

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (18)
    As of 2014-07-11 14:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (227 votes), past polls