[sf-perl] Plucking from a string
Sergey Fetisov
sergey.fetisov at ask.com
Thu May 22 15:24:38 PDT 2008
Hi,
Just do it in a Perl-oriented style - use the regular expressions:
#!/usr/bin/perl
use warnings;
use strict;
my $theline = "vvvbcd=7wwwbcd=88xxxbcd=999yyybcd=1000zzz";
my $TARGETSTR = "bcd=";
while ($theline =~ /($TARGETSTR\d+)/gi) { print "$1\n"; }
Regards,
Sergey
________________________________
From: sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org
[mailto:sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org] On Behalf
Of Neil Heller
Sent: Thursday, May 22, 2008 3:11 PM
To: 'San Francisco Perl Mongers User Group'
Subject: [sf-perl] Plucking from a string
I just wrote the code below and was wondering how I could make a inner
loop A LOT simpler.
I would appreciate any suggestions.
Basically the code works like this:
Given an arbitrary string, that string contains a single identifier (in
this case "bcd=") followed by a numeric value.
I would like to pull out the identifier and the value so the resulting
display is (for example):
bcd=7
bcd=88
bcd=999
bcd=1000
Neil Heller
#! usr/bin/perl
use warnings;
use strict;
my $counter = 0;
my $theline = "vvvbcd=7wwwbcd=88xxxbcd=999yyybcd=1000zzz";
my $TARGETSTR = "bcd=";
my $spot = 0;
my $done = 0;
my $fragment;
my ($inner_done, $inner_char, $inner_count);
my $NUMERIC = "0123456789";
while (!$done) {
$spot = index $theline, $TARGETSTR;
if ($spot == -1) {
$done = 1;
} else {
$fragment = substr $theline, $spot;
print $TARGETSTR;
$inner_done = 0;
$inner_count = 4;
while (!$inner_done) {
$inner_char = substr $fragment, $inner_count, 1;
if ((index $NUMERIC, $inner_char) > -1) {
print $inner_char;
$inner_count++;
} else {
$inner_done = 1;
}
}
print "\n";
$fragment = substr $fragment, 5;
$theline = $fragment;
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20080522/dde7ea92/attachment.html
More information about the SanFrancisco-pm
mailing list