CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
How to match a similiar word in the line and parse that word?

 



cuboidgraphix
User

Mar 23, 2009, 2:36 PM

Post #1 of 4 (1589 views)
How to match a similiar word in the line and parse that word? Can't Post

Hi guys,
Been a while since I been in here ... really need ya'lls help again.

I have an output assigned as an array variable. I have stripped the array of it's '\n' and I'm now trying to process the line.

What I'm trying to do, is try to match a particular string in the line, and parse the word in that line.

For eg:
I want to match a date..

$date = '090323';

to the line..

$data = "R081110000002JF R090323000450JF R081112000045JF R081210000077JF";


Now I would like my output to be..

R090323000450JF

after it's been matched and parsed.

So far I have this..


Code
if($data =~ /R$date/){ 
$word = substr($data,0,0); # This is where I'm stuck.
print $word;
}


Fish ... Kevin... can any of you guys help me here?


KevinR
Veteran


Mar 23, 2009, 2:45 PM

Post #2 of 4 (1585 views)
Re: [cuboidgraphix] How to match a similiar word in the line and parse that word? [In reply to] Can't Post

One possible way:


Code
$date = '090323'; 
$data = "R081110000002JF R090323000450JF R081112000045JF R081210000077JF";
my ($parsed) = $data =~ /(\bR$date[^ ]+)/;
print $parsed;

-------------------------------------------------


cuboidgraphix
User

Mar 23, 2009, 3:14 PM

Post #3 of 4 (1582 views)
Re: [KevinR] How to match a similiar word in the line and parse that word? [In reply to] Can't Post

Thanks Kevin.. it worked. I have much more to go but thanks for helping me with this obstacle.


cuboidgraphix
User

Mar 23, 2009, 3:23 PM

Post #4 of 4 (1581 views)
Re: [KevinR] How to match a similiar word in the line and parse that word? [In reply to] Can't Post


In Reply To
One possible way:


Code
$date = '090323'; 
$data = "R081110000002JF R090323000450JF R081112000045JF R081210000077JF";
my ($parsed) = $data =~ /(\bR$date[^ ]+)/;
print $parsed;



Hi Kevin...
For some reason looking at your code struck a little match stick in my head. I came up with this script which you guys had previously written a while back.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::ParseWords;

my $date = '090323';
my $data = "R081110000002JF R090323000450JF R081112000045JF R081210000077JF R090323004670JF";

my @words = &quotewords('[\s]+', 0, $data);
my $i = 0;
foreach my $word(@words){
if($word =~ /R$date/){
print "$word \n";
}
$i++;
}



Thanks again guys.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives