Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rearranging of a textfile

Status
Not open for further replies.

spydermonkey

Programmer
May 24, 2004
31
US
I've been working on this for three days and I'm getting so lost and frustrated and was wodnering if anyone could offer solutions or advice on how this could be accomplished.


There is a text file that looks like this
Code:
Other Mahoning County $154,900 
Columbiana, Lake Arrowhead 
Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 
330- 702-8787  
702-8787
====

Campbell  $400 
***BUY With “0” Down or land Contract. $400/mo. completely remodeled 3 Bedroom Home in good area near High School. 291 Sanderson Ave. 330-759-2420 or 
cashflowforlife.com 
====

Learn how you can stop renting and buy your own home, even with $0 down and bad credit. Pre-recorded message explains how to order your special FREE report. 
CALL 24 HOURS/DAY 
Toll Free 1-888-534-9659 ID#3006 
or visit our website at 
zerodownhousesforsale.com

I need to reorder each ==== separated content to look like:
Code:
phone1, phone2 (if it exists), $price, everything else

So while passing through the FIRST sample from above, it would be rewritten as
Code:
330-702-8787,702-8787,$154,900,Other Mahoning County 
Columbiana, Lake Arrowhead Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from . A must see!

The phone numbers go first, if a 2nd number doesn't exist it'll be produce ,, so it's no problem. It matches only the first 2 numbers and the first $price, the rest is "other data".<p>

The snippet below works on the money but the problem is, I lose ALL the text other than the money for that time. I need all the text saved in a variable so I can re-order everything.

Any help would be much appreciated!

MY CODE
Code:
#!/usr/bin/perl

use warnings;
use strict;

#####################
# configuration section
#####################

my $readfrom = "test.txt";
## change the above to the file you're reading from

my $writeto = "output.txt";
## change the above to the file you're writing to

#####################
# do not edit below this line
#####################

$/="====\n\n";

open (READFROM, "$readfrom") or die "Cannot open $readfrom: $!";

open (WRITETO, ">$writeto") or die "Cannot open $writeto: $!";

while ( <READFROM> ) 
{ 
  chomp; 


my $price = $_;
for ( 0 .. 1)
{
$price =~ m/
        (
          \$
          (?:\d{1,3},?)+
          (?:\.\d{0,2})?
          (?![.\d])
         )
               /x;

$price = "$1";
}
print "$price\n";

}

close (WRITETO) or die "Cannot close $writeto: $!";

close (READFROM) or die "Cannot close $readfrom: $!";
 
$buffer = 'Other Mahoning County $154,900
Columbiana, Lake Arrowhead
Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see!
330- 702-8787
702-8787
====
Campbell $400
***BUY With “0” Down or land Contract. $400/mo. completely remodeled 3 Bedroom Home in good area near High School. 291 Sanderson Ave. 330-759-2420 or
cashflowforlife.com
====
Learn how you can stop renting and buy your own home, even with $0 down and bad credit. Pre-recorded message explains how to order your special FREE report.
CALL 24 HOURS/DAY
Toll Free 1-888-534-9659 ID#3006
or visit our website at
zerodownhousesforsale.com ';


$buffer =~s/\t|\n|\r//g;
$buffer =~s/ -/-/g;
$buffer =~s/- /-/g;

@data = split(/====/, $buffer);

foreach $item (@data){
@ad = split(/ /, $item);

$price = '';
foreach $item2 (@ad){
if ($item2 =~m/\d\d\d-\d\d\d-\d\d\d\d|\d\d\d-\d\d\d\d/){
push(@phone, $item2);
$item2 = '';
}
if ($item2 =~m/\$/){
$price = $item2;
$item2 = '';
}
}
print "@phone $price @ad\n====\n";


}
 
sorry didn't look at your data output $item2 doesn't need to be set to nothing after finding a match.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top