in reply to print number of characters after specific character

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.

Replies are listed 'Best First'.
Re^2: print number of characters after specific character
by johngg (Canon) on Sep 12, 2013 at 16:31 UTC

    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