[sf-perl] Plucking from a string
Neil Heller
nheller at silcon.com
Thu May 22 15:11:06 PDT 2008
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/1419e2f6/attachment.html
More information about the SanFrancisco-pm
mailing list