amparida has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to print 10 characters after 47 on the input string. PSB my expectation. Could anyone please help on this?
Input: 471234973798375754773971832374974447889743725345932 Output: 47123497379837575 47739718323749744 47889743725345932
Regards, Am

Replies are listed 'Best First'.
Re: print number of characters after specific character
by kcott (Archbishop) on Sep 12, 2013 at 01:56 UTC

    G'day amparida,

    Firstly, PerlMonks is not a code-writing service! It is not OK to post a spec and just expect someone here to do your work for you: see Do Your Own Work in "How (Not) To Ask A Question".

    The tools you need for this task may depend on context (which you haven't provided). Take a look at index, to locate instances of 47 within your string, and substr to capture the substring you want. Another option would be to use a regular expression to capture the substrings: see "perlretut - Perl regular expressions tutorial".

    I note you say "10 characters after 47", but the "Output:" examples have 15 characters in each instance: you'll need to determine which is correct.

    If you subsequently run into difficulties, feel free to ask here, but ensure you follow the guidelines in "How do I post a question effectively?".

    -- Ken

Re: print number of characters after specific character
by choroba (Cardinal) on Sep 12, 2013 at 09:27 UTC
    Your expectation does not meet the specification, unless you are using the heptadecimal notation.

    You can use index to find a substring in a string. Its third argument tells Perl where to start searching.

    #!/usr/bin/perl use warnings; use strict; my $input = "471234973798375754773971832374974447889743725345932"; my $pos = 0; while (-1 != ($pos = index $input, '47', $pos)) { print substr($input, $pos++, 10), "\n"; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      thanks it is working fine.
Re: print number of characters after specific character
by mcshell (Novice) on Sep 12, 2013 at 01:51 UTC
    $/=\17; print $_,"\n" while <DATA>;
    However, I do not quite understand what you mean
Re: print number of characters after specific character
by hdb (Monsignor) on Sep 12, 2013 at 09:17 UTC

    "47 and the 10 characters thereafter" would translate into the regular expression /47.{10}/. Now you only need to repeatedly apply this regex to your string and capture/print the desired bits as described in http://perldoc.perl.org/perlre.html#Capture-groups.

Re: print number of characters after specific character
by AnomalousMonk (Archbishop) on Sep 12, 2013 at 07:20 UTC
    PSB my expectation.

    In addition to agreeing with kcott's apt reply, I'm puzzled by the quoted sentence. Do any of the following definitions apply to PSB, or maybe "Please Stop Berating"?

      Maybe "Please See Below" ?

        ++ Aha!

      Perl Script Be my expectation.

        ++ Even better!!

Re: print number of characters after specific character
by johngg (Canon) on Sep 12, 2013 at 10:06 UTC

    Perhaps split using a look-ahead?

    $ perl -E ' > $str = q{471234973798375754773971832374974447889743725345932}; > say for split m{(?=47)}, $str;' 47123497379837575 47739718323749744 47889743725345932 $

    Cheers,

    JohnGG

Re: print number of characters after specific character
by ww (Archbishop) on Sep 12, 2013 at 13:10 UTC
    Was having fun with variations on johngg's use of a lookahead (novel, at least to me; ++). Then took one version of OP's spec -- the 10 chars AFTER each "47" -- as my target and came up with this (awkward and ugly?, besmirched with globals; non-PBP) approach:
    #!/usr/bin/perl use 5.016; use warnings; # D:\_Perl_\PMonks\1053693.pl my $str = q/471234xx3798375754712345678901234547zyxabcdefghjkl/; my ($arr, @arr1, $arr1, $i); my @arr = split /47/, $str; for $arr ( @arr ) { @arr1 = split //, $arr; for $i (0 .. 9) { no warnings 'uninitialized'; $arr1 .= $arr1[$i]; use warnings; } say "$arr1\n"; undef $arr1; } =head output: 1234xx3798 1234567890 zyxabcdefg =cut

    Would welcome suggestions for using this approach more elegantly (for the spec '10 char after "47"').

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

      With your double split, rather than concatenation in a loop I'd go for join'ing an array slice, although substr seems more suited to the task.

      $ perl -E ' $str = q{471234973798375754773971832374974447889743725345932}; say for map { join q{}, ( split m{} )[ 0 .. 9 ] } split m{47}, $str; say for map { substr $_, 0, 10 } split m{47}, $str;' 1234973798 7397183237 8897437253 1234973798 7397183237 8897437253 $

      I hope this is of interest.

      Cheers,

      JohnGG