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: Beginner:
parse by matching

 



t6
New User

Aug 18, 2011, 7:52 AM

Post #1 of 3 (408 views)
parse by matching Can't Post

Hey guys, i'm very new to pearl trying to figure this one out having a really tough time. I have a really long and complex string (actually I have an array of these) from which I need to extract 2 pieces of information. The information I want is prefixed by "Name: " and "Date: ". I got some help which prints me the information I need, but I need to parse those 2 pieces of information into 2 separate variables, $name and $date.

Here is my code:


Code
 foreach( @complexstring ) { 
/^(Name|Date):\s+/i && print $_, "\n";
}


With 2 elements in my array, this prints me:
Name: Carly
Date: Jun 07 2011
Name: Jessica
Date: Jun 29 2011

I need to parse the 'name' and 'date' into variables $name and $date within the for loop.

Please help. Thank you.

(This post was edited by t6 on Aug 18, 2011, 7:53 AM)


BillKSmith
Veteran

Aug 18, 2011, 8:57 AM

Post #2 of 3 (405 views)
Re: [t6] parse by matching [In reply to] Can't Post

Either the array @complexstring does not contain what you described or this is not the code your are executing! Print and post all of @complexstring for the two samples.
Good Luck,
Bill


wickedxter
User

Aug 18, 2011, 4:20 PM

Post #3 of 3 (398 views)
Re: [t6] parse by matching [In reply to] Can't Post

try this regex change


Code
 
/^(Name|Date):\s+(.*)/


to get the data after the Name or date and : its saved in the $1 variable.... so

my $NameDate = $1;


this will match if it starts with Name or Date followed by a space 1 or more times then captures everthing beyond that the * means 0 or more. only thing is if you get data in lower cases it will skip it.


(This post was edited by wickedxter on Aug 18, 2011, 4:23 PM)

 
 


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

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