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:
pattern matching

 



johnchandler32
New User

Nov 5, 2012, 1:48 AM

Post #1 of 7 (1570 views)
pattern matching Can't Post

Hello,

I am trying to complete a script that will print out matching pattern compared to the next line. I am having a problem getting the correct output because not all the fields are exact matches.

I want to compare if a truck was brought and sold at the same time with the same price.

File:

"Truck 1212" BUY 8990.00 20 0 16054700
"Truck 1212" SELL 8990.00 0 20 16054700
"Truck 1212" SELL 8995.00 0 8 16083000
"Truck 1212" SELL 8995.00 0 10 16083000
"Truck 1212" SELL 8980.00 0 6 16294400
"Truck 1212" SELL 8980.00 0 19 16294400
"Truck 1212" SELL 9010.00 0 1 08053500
"Truck 1212" SELL 9010.00 0 2 08053500
"Truck 1212" SELL 9010.00 0 2 08053500
"Truck 1212" BUY 9010.00 2 0 08053500
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9010 0 1 220507
"Doll 1212" BUY 9010 1 0 220507


Output:
"Truck 1212" BUY 8990.00 20 0 16054700
"Truck 1212" SELL 8990.00 0 20 16054700

"Doll 1212" SELL 9010 0 1 220507
"Doll 1212" BUY 9010 1 0 220507


Please help!


rovf
Veteran

Nov 5, 2012, 2:20 AM

Post #2 of 7 (1569 views)
Re: [johnchandler32] pattern matching [In reply to] Can't Post

I don't quite understand your problem. You say that not all field are exact matches, but in the example you gave, the price- and date-fields *are* exact matches.

Also, you don't say exactly where you are stuck.


johnchandler32
New User

Nov 5, 2012, 2:29 AM

Post #3 of 7 (1568 views)
Re: [rovf] pattern matching [In reply to] Can't Post

Not all the fields matches. Field $2 $4 $5 do not match the next row in the output.

I used awk to get the following out from a much bigger file.


-------1---------------2-----3-------4---5-------6-----
"Truck 1212" BUY 8990.00 20 0 16054700
"Truck 1212" SELL 8990.00 0 20 16054700
"Truck 1212" SELL 8995.00 0 8 16083000
"Truck 1212" SELL 8995.00 0 10 16083000
"Truck 1212" SELL 8980.00 0 6 16294400
"Truck 1212" SELL 8980.00 0 19 16294400
"Truck 1212" SELL 9010.00 0 1 08053500
"Truck 1212" SELL 9010.00 0 2 08053500
"Truck 1212" SELL 9010.00 0 2 08053500
"Truck 1212" BUY 9010.00 2 0 08053500
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9000 0 1 220507
"Doll 1212" SELL 9010 0 1 220507
"Doll 1212" BUY 9010 1 0 220507


Output:
"Truck 1212" BUY 8990.00 20 0 16054700
"Truck 1212" SELL 8990.00 0 20 16054700

"Doll 1212" SELL 9010 0 1 220507
"Doll 1212" BUY 9010 1 0 220507


rovf
Veteran

Nov 5, 2012, 3:48 AM

Post #4 of 7 (1567 views)
Re: [johnchandler32] pattern matching [In reply to] Can't Post


Quote
Not all the fields matches. Field $2 $4 $5 do not match the next row in the output.


Well, field 2 is either BUY or SELL, which is a good thing, because otherwise you can't correlate the lines. Fields 4 and 5 are just the reverse, but are you interested in their content? I think you need only fields 1, 3 and 6 for identifying the transaction, provided it is really the case - as you seem to assume - that things are sold always on the same day as they are bought, which is, IMO, a strange assumption.

For analyzing the lines, I would first split them into individual fields. Perhaps it is best to first extract field 2 (because this field is different, in that it can contain embedded spaces, and is delimited by quotes), and then use split to split the remaining line into individual fields. You can then use for your comparision between two successive rows exactly those fields you are interested in.


Laurent_R
Veteran / Moderator

Nov 5, 2012, 4:14 AM

Post #5 of 7 (1565 views)
Re: [johnchandler32] pattern matching [In reply to] Can't Post

Just split your line on spaces into an array @previous, read next line and split it into @current, compare the relevant fields between current and previous, print the whole array (or line or whatever you want to print exactly) if the relevant fields match. Then copy @current onto @previous, read next line, split it to @current and so on in a loop.


rovf
Veteran

Nov 5, 2012, 9:21 AM

Post #6 of 7 (1555 views)
Re: [Laurent_R] pattern matching [In reply to] Can't Post


Quote
Just split your line on spaces


This is not sufficient, since the first field contains embedded spaces.

I would first split on the double-quotes (which gives us the first field, and all the remaining ones lumped together), and then split again the remaining string on spaces.

We can do this too (and likely somewhat more efficient) with a single regexp, but I personally believe that the double-split is maybe a bit easier to understand, especially for a beginner.


Laurent_R
Veteran / Moderator

Nov 5, 2012, 10:03 AM

Post #7 of 7 (1553 views)
Re: [rovf] pattern matching [In reply to] Can't Post


In Reply To

Quote
Just split your line on spaces


This is not sufficient, since the first field contains embedded spaces.


Yes, I saw that, but t does not really matter since it is only one space (at least in the data sample above). We just need to pick up the right field subscript.

But it can also be done the way you said.

 
 


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

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