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?".
| [reply] [d/l] |
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";
}
| [reply] [d/l] |
|
|
thanks it is working fine.
| [reply] |
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 | [reply] [d/l] |
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.
| [reply] [d/l] |
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"?
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
Re: print number of characters after specific character
by johngg (Canon) on Sep 12, 2013 at 10:06 UTC
|
$ perl -E '
> $str = q{471234973798375754773971832374974447889743725345932};
> say for split m{(?=47)}, $str;'
47123497379837575
47739718323749744
47889743725345932
$
| [reply] [d/l] |
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.
| [reply] [d/l] |
|
|
$ 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.
| [reply] [d/l] |