[sf-perl] Plucking from a string

Neil Heller nheller at silcon.com
Thu May 22 16:40:26 PDT 2008

You win the gold star!

One question though.  In the line


while ($theline =~ /($TARGETSTR\d+)/gi) {  print "$1\n"; }


why did you use the parens "(" and ")" inside the real expression?



Neil Heller



From: sanfrancisco-pm-bounces+nheller=silcon.com at pm.org
[mailto:sanfrancisco-pm-bounces+nheller=silcon.com at pm.org] On Behalf Of
Sergey Fetisov
Sent: Thursday, May 22, 2008 3:25 PM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] Plucking from a string




Just do it in a Perl-oriented style - use the regular expressions:


use warnings;
use strict;

my $theline = "vvvbcd=7wwwbcd=88xxxbcd=999yyybcd=1000zzz";
my $TARGETSTR = "bcd=";

while ($theline =~ /($TARGETSTR\d+)/gi) {  print "$1\n"; }








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):






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;


            } 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/4a72ed45/attachment.html 

More information about the SanFrancisco-pm mailing list