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: Regular Expressions:
how can I extract from file with info separated by newline

 



jujea
New User

Jun 4, 2008, 2:33 PM

Post #1 of 3 (3108 views)
how can I extract from file with info separated by newline Can't Post

how can I extract the $firstname, $lastname and rest from a file that has each value on a newline. the file contains more than one entry and are separated by ----------- for example


First Name : James
Last Name : Segrove
DOB : 07/26/1970
Email : jsegrove@ntlworld.com
Password : tramontana
Address : 50 Chesilton Crescent
City : Fleet
Zip : GU52 6PB
State : Hanmshire
Country : United Kingdom
Phone : 01252 616106
---------------------------------------------------------------------------------------------------------


(This post was edited by perlguru_admin on Jun 8, 2008, 9:57 AM)


rajiv_chennai
Novice

Jun 4, 2008, 10:07 PM

Post #2 of 3 (3085 views)
Re: [jujea] how can I extract from file with info separated by newline [In reply to] Can't Post

Lets try this code,i think this what you are expecting.


Code
#!/usr/bin/perl 

my @array=<DATA>;

my @first_array=grep(/First Name :(.)/,@array);

map{~s/First Name :(.)/$1/}@first_array;

my @last_array=grep(/Last Name :(.)/,@array);

map{~s/Last Name :(.)/$1/}@last_array;


print @first_array;

print "\n";

print @last_array;


__END__

First Name : James
Last Name : Segrove
DOB : 07/26/1970
Email : jsegrove@ntlworld.com
Password : tramontana
Address : 50 Chesilton Crescent
City : Fleet
Zip : GU52 6PB
State : Hanmshire
Country : United Kingdom
Phone : 01252 616106
---------------------------------------------------------------------------------------------------------


Regards
Rajiv


(This post was edited by perlguru_admin on Jun 8, 2008, 9:57 AM)


KevinR
Veteran


Jun 5, 2008, 11:56 AM

Post #3 of 3 (3063 views)
Re: [rajiv_chennai] how can I extract from file with info separated by newline [In reply to] Can't Post

Define the input record seperator and perl will read the records into an array properly from the file. Then parse each element of the array into fields.
-------------------------------------------------

 
 


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

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