Help needed for perl rookie

G

GRLCOPM

I am new to perl, but so far have had decent success in writing/modifying
code to do what I want to do. However I am stuck trying to modify the
following code. I am sure the solution is quite simple, but I can't
completely figure out what this piece of code does. I think it is just
matching up a data pattern but this is an area I am unfamiliar with.

All I want to do is change the format of the data file from example #1 to
example #2 and need this section of code to work with the new format. I
would be grateful for any help provided in understanding what this piece of
code does and suggestions on the modification needed.

If more information or a larger chunk of the code is needed please let me
know and I will provide.

EXAMPLE #1 - Current format of data file:
0000000050 20041227 0000000003 'my-page.shtml'
0000000054 20041227 0000000004 'another-page.shtml'
0000000020 20041227 0000000003 'yet-another-page.shtml'

EXAMPLE #2 - New format of data file:
0000000050|20041227|0000000003|my-page.shtml
0000000054|20041227|0000000004|another-page.shtml
0000000020|20041227|0000000003|yet-another-page.shtml

Current code that reads original data format:

&LockOpen (COUNT,"$AccessFile");
$location = tell COUNT;
while ($line = <COUNT>) {
if (($acc,$day,$dayacc,$uri) = ($line =~ /^(\d+) (\d+) (\d+) '(\S+)'$/)) {
if ($uri eq $doc_uri) {
last;
}
}
last if ($uri eq $doc_uri);
$location = tell COUNT;
$acc = 0;
$dayacc = 0;
}


Thanks!

PM
 
I

ioneabu

GRLCOPM said:
I am new to perl, but so far have had decent success in writing/modifying
code to do what I want to do. However I am stuck trying to modify the
following code. I am sure the solution is quite simple, but I can't
completely figure out what this piece of code does. I think it is just
matching up a data pattern but this is an area I am unfamiliar with.

All I want to do is change the format of the data file from example #1 to
example #2 and need this section of code to work with the new format. I
would be grateful for any help provided in understanding what this piece of
code does and suggestions on the modification needed.

If more information or a larger chunk of the code is needed please let me
know and I will provide.

EXAMPLE #1 - Current format of data file:
0000000050 20041227 0000000003 'my-page.shtml'
0000000054 20041227 0000000004 'another-page.shtml'
0000000020 20041227 0000000003 'yet-another-page.shtml'

EXAMPLE #2 - New format of data file:
0000000050|20041227|0000000003|my-page.shtml
0000000054|20041227|0000000004|another-page.shtml
0000000020|20041227|0000000003|yet-another-page.shtml

looks like you are replacing the spaces after the numbers with a '|'
and removing the single quotes.

s/(\d+)\s/$1|/g;
s/'//g;

maybe
 
G

GRLCOPM

From: (e-mail address removed)
Organization: http://groups.google.com
Newsgroups: comp.lang.perl.misc
Date: 27 Dec 2004 14:17:37 -0800
Subject: Re: Help needed for perl rookie


looks like you are replacing the spaces after the numbers with a '|'
and removing the single quotes.

s/(\d+)\s/$1|/g;
s/'//g;

Yes, that is how I have changed the format of the data file. Replaced the
spaces with | and removed the single quotes from the last item on the line.

Can someone please explain what the the following line of code does and what
the replacement would be?

if (($acc,$day,$dayacc,$uri) = ($line =~ /^(\d+) (\d+) (\d+) '(\S+)'$/)) {

Thanks

Patrick
 
J

Jim Keenan

GRLCOPM said:
I am new to perl,


For your future reference: Read the posting guidelines for this Usenet
news group: http://mail.augustmail.com/~tadmc/clpmisc.shtml

As the guidelines advise, put your real Perl subject in the "Subject"
line. There's no need to apologize for being a rookie provided you make
a genuine effort to solve the problem yourself prior to posting. But
the fact that you're new to Perl, should you wish to include it, is best
included in the body of your posting. Don't take up valuable Net real
estate by wasting it on the Subject line. HTH.

Jim Keenan
 
G

GRLCOPM

From: Jim Gibson <[email protected]>
Organization: Newsfeed.com http://www.newsfeeds.com 100,000+ UNCENSORED
Newsgroups.
Newsgroups: comp.lang.perl.misc
Date: Mon, 27 Dec 2004 15:05:04 -0800
Subject: Re: Help needed for perl rookie



Starting from the inner =~ operator and working outwards:

Thanks Jim!

At first I missed your reply this morning so please excuse my re-post of the
question.

The line you provided works exactly as desired and in addition I really
appreciate you taking your time to explain the code. Stumbling around in the
dark I managed to get pretty close, but failed to escape the | char.

Thanks again!

Happy Holidays,

Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top