Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to fetch 1,2,3, from chapter

by ww (Bishop)
on Apr 27, 2013 at 01:44 UTC ( #1030904=note: print w/ replies, xml ) Need Help??


in reply to how to fetch 1,2,3, from chapter

Alternately,

#!/usr/bin/perl use 5.016; # 1030798ALT my $str = "chapter 1,2,3"; # out: "chapter 1,chapter 2,chapter 3" my ($chapter, @num); if ( $str =~ /^(chapter) / ) { $chapter = $1; # say "\$chapter: |$chapter|"; } my @chapnums = ( $str =~ /(\d[,]*)/gc); if ( @chapnums) { push @num,@chapnums; } for my $num(@num) { # say "DEBUG \$#num: $#num"; # if ( $1 != (1 + $#num) ) { # print "\$chapter $num" . ","; # } elsif ( $1 == (1 + $#num) ){ print "$chapter $num"; }

output: chapter 1,chapter 2,chapter 3

Less plodding, perhaps less mad, but still a longform version of suggestions above.

virudinesh: your title and narrative don't agree about whether you want a comma after "chapter 3" -- an internal contradition that suggests carelessness on your part, and makes it difficult for the Monks to offer a definitive answer. It's not that doing it either way is hard; it's that it's hard to know what you want, and thus, what help you need.


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


Comment on Re: how to fetch 1,2,3, from chapter
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-10-23 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls