Current location: Hot Scripts Forums » Programming Languages » Perl » parsing in Perl


parsing in Perl

Reply
  #1 (permalink)  
Old 06-04-08, 12:16 PM
hiteshiyer hiteshiyer is offline
New Member
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile parsing in Perl

Hello,

I'm a novice perl user, making the best out of existing scripts written by gurus and need some help.

I'm trying to parse the follwoing data so that data after ";" is put into a different column within excel.

US;0;Search;,"741,305"
US;0;Item_Active_None;WDVW,"80,539"
US;0;All matching items for sale.;,"49,117"

I'm using the following parsing codes, but none of these are picking up the above lines of data.

s/^"(.{2,4});([0-9]{1,3});(.*?);(.*?),"$/$1|$2|$3|$4/g;
s/^"(.{2,4});;(.*?);(.*?),"$/$1||$2|$3/g;

s/^"(.{2,4});([0-9]{1,3});(.*?);(.*?)"$/$1|$2|$3|$4/g;
s/^"(.{2,4});;(.*?);(.*?)"$/$1||$2|$3/g;

s/^"(.{2,4});([0-9]{1,3});(.*?),"$/$1|$2|$3|/g;
s/^"(.{2,4});;(.*?),"$/$1||$2|/g;

s/^"(.{2,4});([0-9]{1,3});(.*?)"$/$1|$2|$3|/g;
s/^"(.{2,4});;(.*?)"$/$1||$2|/g;

s/^(.{2,4});([0-9]{1,3});"(.*?);(.*?),"$/$1|$2|$3|$4/g;
s/^(.{2,4});;"(.*?);(.*?),"$/$1||$2|$3/g;

s/^(.{2,4});([0-9]{1,3});"(.*?);(.*?)"$/$1|$2|$3|$4/g;
s/^(.{2,4});;"(.*?);(.*?)"$/$1||$2|$3/g;

s/^(.{2,4});([0-9]{1,3});"(.*?),"$/$1|$2|$3|/g;
s/^(.{2,4});;"(.*?),"$/$1||$2|/g;

s/^(.{2,4});([0-9]{1,3});"(.*?)"$/$1|$2|$3|/g;
s/^(.{2,4});;"(.*?)"$/$1||$2|/g;

s/^(.{2,4});([0-9]{1,3});(.*?);(.*?)$/$1|$2|$3|$4/g;
s/^(.{2,4});;(.*?);(.*?)$/$1||$2|$3/g;

s/^(.{2,4});([0-9]{1,3});(.*?)$/$1|$2|$3|/g;
s/^(.{2,4});;(.*?)$/$1||$2|/g;

Any help with the above would be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 06-04-08, 12:56 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Have you tried using the "split" function. Without seeing your code, it is rather hard to assist you. (All you have given us is regexes that don't work. Are you slurping the data from a file or reading it in line by line?)

Have you tried asking the *gurus* that you got this code from?
================================================== ==============

A very useful function in Perl is split, which splits up a string and places it into an array. The function uses a regular expression and as usual works on the $_ variable unless otherwise specified.

The split function is used like this:

$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);

which has the same overall effect as

@personal = ("Caine", "Michael", "Actor", "14, Leafy Drive");

If we have the information stored in the $_ variable then we can just use this instead

@personal = split(/:/);

If the fields are divided by any number of colons then we can use the RE codes to get round this. The code

$_ = "Capes:Geoff::Shot putter:::Big Avenue";
@personal = split(/:+/);

is the same as

@personal = ("Capes", "Geoff",
"Shot putter", "Big Avenue");

But this:

$_ = "Capes:Geoff::Shot putter:::Big Avenue";
@personal = split(/:/);

would be like

@personal = ("Capes", "Geoff", "",
"Shot putter", "", "", "Big Avenue");

A word can be split into characters, a sentence split into words and a paragraph split into sentences:

@chars = split(//, $word);
@words = split(/ /, $sentence);
@sentences = split(/\./, $paragraph);

In the first case the null string is matched between each character, and that is why the @chars array is an array of characters - ie an array of strings of length 1.
__________________
Whatever you decide, you should make sure best security methods are used and practiced. Should you really need more help, PM me.
Reply With Quote
  #3 (permalink)  
Old 06-04-08, 01:23 PM
hiteshiyer hiteshiyer is offline
New Member
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you! Unfortunately I can not ask the person who created the script as he is no longer around.
I am getting the data from a file and using the variable $_ to place it into an array.

I guess what's confusing me is that why aren't these fields
( US;0;Search;,"741,305"
US;0;Item_Active_None;WDVW,"80,539"
US;0;All matching items for sale.;,"49,117" )
being recognized by any of the wildcards used in the switch commands.

The above switch commands do recognize the following field
( US;;ViewItem_Active_Outbid;STRK:MEBIDX:IT ) and separates them at semicolons

Thank you again for all your help.

Regards,
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Perl Parsing cougar Perl 6 02-14-07 02:43 AM
Perl for a dummy kragnesb414 Perl 1 01-31-06 04:34 AM
Perl Parsing Script DeerHunter Perl 11 04-29-05 04:11 PM
Perl FormMail Problem ennanguyen2002 Perl 2 03-09-05 04:47 PM
Perl nOOb needs help parsing a file egiggey Perl 1 11-07-03 10:50 AM


All times are GMT -5. The time now is 04:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.