Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

how to fetch 1,2,3, from chapter

by virudinesh (Acolyte)
on Apr 26, 2013 at 10:06 UTC ( #1030798=perlquestion: print w/ replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

$f="chapter 1,2,3"; if($f=~m/chap( |[a-z])*([0-9|,0-9]+)/ i) { print "$f\n" ; #for split /,/,$2; } output needs : chapter 1,chapter 2,chapter 3 but does't work...

Comment on how to fetch 1,2,3, from chapter
Download Code
Re: how to fetch 1,2,3, from chapter
by choroba (Abbot) on Apr 26, 2013 at 10:12 UTC
    Using glob:
    #!/usr/bin/perl use warnings; use strict; my $f = 'chapter 1,2,3'; $f =~ s/ ([0-9,]+)/\\ {$1}/; print "$_\n" for glob $f;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to fetch 1,2,3, from chapter
by DrHyde (Prior) on Apr 26, 2013 at 10:31 UTC

    You told us what the output needs to be in one case but not what the input is, or what it should do in the general case. I suggest that you start with this, and then modify it once you've figured out what your real requirements are:

    print " chapter 1,chapter 2,chapter 3\n";

    You can't expect us to read your mind.

Re: how to fetch 1,2,3, from chapter
by hdb (Parson) on Apr 26, 2013 at 11:01 UTC
    $f="chapter 1,2,3"; if($f=~m/chap( |[a-z])*([,0-9]+)/i) { print join ", ", map { "chapter $_" } split /,/,$2; }
Re: how to fetch 1,2,3, from chapter
by Random_Walk (Parson) on Apr 26, 2013 at 13:05 UTC

    How about the fun way :)

    > perl -e\ "print join', ',map {\"Chapter $_\"} splice@{[split/\D+/,'Chap 1,2,3'] +},1" Chapter 1, Chapter 2, Chapter 3

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: how to fetch 1,2,3, from chapter
by ww (Bishop) on Apr 26, 2013 at 17:29 UTC
    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!

Re: how to fetch 1,2,3, from chapter
by ww (Bishop) on Apr 27, 2013 at 01:44 UTC
    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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-08-21 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls