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!

tricky substituions 1

Status
Not open for further replies.

spydermonkey

Programmer
May 24, 2004
31
US
This will be a lengthy post and I appologize, the question is lengthy but the problem shouldn't be.

I have a config txt file where I have something like:
Code:
bdroom || bedroom
clst || closet
this string || ---
bowl of || plate of
apple || pear
php || ---
love || hate

I am building an array to read each new line as it's own value. Then I have a regex to separate the left side and the right side of || into their own array.

Reason for this is.. I have a real text file I need to perform s/// for. Every time the left side of || appears in the code it needs to be replaced with the right side.

I test printed both @first_half and @second_half and get EXACTLY what I needed. The problem is in the output file, it's hard to explain but it's not right. It should print out the file as it was originally with only the s/// changes.


This is the script itself
Code:
$/="\n";


my @split_strings;
my @first_half;
my @second_half;

#########
# open our replace file
#########
open (READFROM, "$readfrom") or die "Cannot open $readfrom: $!";


#########
# Prepare our file into an array
#########
while ( <READFROM> ) 
{ 
  chomp;
  push(@split_strings,$_);
}

close (READFROM) or die "Error: $!";




########
# our separator to split the array in two
########
my $sep = qr/\Q||\E/;
#my $sep = "\Q||";


##########
# begin separating our data
##########
foreach my $line (@split_strings)
{ 

  my ($first,$second) = $line =~ /^(.+)\s+$sep\s+(.+)$/; 
  push @first_half, $first; 
  push @second_half, $second; 

}


#######
# remove our ---'s we had to setup
#######
foreach (@first_half) { print "$_\n"; }

print "\n";

foreach my $line (@second_half) 
{ 
   #$line =~ s/---/ /; 
   print "$line\n";
} 

#######
# Open our source and output files
#######
open (SOURCE, "$sourcefile") or die "Cannot open $sourcefile: $!";
open (WRITETO, ">$writeto") or die "Cannot open $writeto: $!";


#######
# All replacements are done here
#######
while ( my $piece = <SOURCE> ) {
    foreach ( 0 .. $#first_half ) {
        $piece =~ s/$first_half[$_]/$second_half[$_]/;
        print WRITETO "$piece\n";
    }
}


#######
# close filehandles
#######
close (SOURCE) or die "Error occured for $sourcefile: $!";
close (WRITETO) or die "Error occured for $writeto: $!";

This is the source file
Code:
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 
====

Boardman $157,000 
COLONIAL/1 ACRE 
WOW! 
We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 
David Realty 330-758-8363  
330-758-8363
====

Boardman  $475 
A BARGAIN - 0 DOWN 
$475 & up/mo. 
2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 
7372 Oregon Trail 
7421 Siera Madre 
All Credit Considered 
Jim Rich Realty 330-783-9300 
====

North Jackson  
JACKSON MEADOWS 
Custom built 3&4 bedroom homes 
330-538-4663 or 330-503-1985  
330-538-4663
====

Dogs  
PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 
====

Austintown  
STANJIM HOMES 
1850 Countryside Drive 
Kingwood Real Estate 793-2010  
793-2010
====

Boardman $339,900 
JUST move in! this home has over 3000 sq-ft; $339,900. 
Cocca Real Estate 330-758-9904  
330-758-9904
====


This is the reulsts:
Code:
You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

You’ll hate this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

====

====

====

====

====

====

====















Other Mahoning County $154,900 

Other Mahoning County $154,900 

Other Mahoning County $154,900 

Other Mahoning County $154,900 

Other Mahoning County $154,900 

Other Mahoning County $154,900 

Other Mahoning County $154,900 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Columbiana, Lake Arrowhead 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

Unique, condo home w/sunroom, fireplace & more! $154,900. Lease from $995. A must see! 

330- 702-8787  

330- 702-8787  

330- 702-8787  

330- 702-8787  

330- 702-8787  

330- 702-8787  

330- 702-8787  

702-8787

702-8787

702-8787

702-8787

702-8787

702-8787

702-8787

====

====

====

====

====

====

====















Campbell  $400 

Campbell  $400 

Campbell  $400 

Campbell  $400 

Campbell  $400 

Campbell  $400 

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 

***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 

***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 

***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 

***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 

***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 

***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 

cashflowforlife.com 

cashflowforlife.com 

cashflowforlife.com 

cashflowforlife.com 

cashflowforlife.com 

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. 

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. 

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. 

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. 

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. 

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. 

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 

CALL 24 HOURS/DAY 

CALL 24 HOURS/DAY 

CALL 24 HOURS/DAY 

CALL 24 HOURS/DAY 

CALL 24 HOURS/DAY 

CALL 24 HOURS/DAY 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

Toll Free 1-888-534-9659 ID#3006 

or visit our website at 

or visit our website at 

or visit our website at 

or visit our website at 

or visit our website at 

or visit our website at 

or visit our website at 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

zerodownhousesforsale.com 

====

====

====

====

====

====

====















Boardman $157,000 

Boardman $157,000 

Boardman $157,000 

Boardman $157,000 

Boardman $157,000 

Boardman $157,000 

Boardman $157,000 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

COLONIAL/1 ACRE 

WOW! 

WOW! 

WOW! 

WOW! 

WOW! 

WOW! 

WOW! 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

David Realty 330-758-8363  

David Realty 330-758-8363  

David Realty 330-758-8363  

David Realty 330-758-8363  

David Realty 330-758-8363  

David Realty 330-758-8363  

David Realty 330-758-8363  

330-758-8363

330-758-8363

330-758-8363

330-758-8363

330-758-8363

330-758-8363

330-758-8363

====

====

====

====

====

====

====















Boardman  $475 

Boardman  $475 

Boardman  $475 

Boardman  $475 

Boardman  $475 

Boardman  $475 

Boardman  $475 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

A BARGAIN - 0 DOWN 

$475 & up/mo. 

$475 & up/mo. 

$475 & up/mo. 

$475 & up/mo. 

$475 & up/mo. 

$475 & up/mo. 

$475 & up/mo. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

7372 Oregon Trail 

7372 Oregon Trail 

7372 Oregon Trail 

7372 Oregon Trail 

7372 Oregon Trail 

7372 Oregon Trail 

7372 Oregon Trail 

7421 Siera Madre 

7421 Siera Madre 

7421 Siera Madre 

7421 Siera Madre 

7421 Siera Madre 

7421 Siera Madre 

7421 Siera Madre 

All Credit Considered 

All Credit Considered 

All Credit Considered 

All Credit Considered 

All Credit Considered 

All Credit Considered 

All Credit Considered 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

Jim Rich Realty 330-783-9300 

====

====

====

====

====

====

====















North Jackson  

North Jackson  

North Jackson  

North Jackson  

North Jackson  

North Jackson  

North Jackson  

JACKSON MEADOWS 

JACKSON MEADOWS 

JACKSON MEADOWS 

JACKSON MEADOWS 

JACKSON MEADOWS 

JACKSON MEADOWS 

JACKSON MEADOWS 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

Custom built 3&4 bedroom homes 

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663 or 330-503-1985  

330-538-4663

330-538-4663

330-538-4663

330-538-4663

330-538-4663

330-538-4663

330-538-4663

====

====

====

====

====

====

====















Dogs  

Dogs  

Dogs  

Dogs  

Dogs  

Dogs  

Dogs  

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

====

====

====

====

====

====

====















Austintown  

Austintown  

Austintown  

Austintown  

Austintown  

Austintown  

Austintown  

STANJIM HOMES 

STANJIM HOMES 

STANJIM HOMES 

STANJIM HOMES 

STANJIM HOMES 

STANJIM HOMES 

STANJIM HOMES 

1850 Countryside Drive 

1850 Countryside Drive 

1850 Countryside Drive 

1850 Countryside Drive 

1850 Countryside Drive 

1850 Countryside Drive 

1850 Countryside Drive 

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

Kingwood Real Estate 793-2010  

793-2010

793-2010

793-2010

793-2010

793-2010

793-2010

793-2010

====

====

====

====

====

====

====















Boardman $339,900 

Boardman $339,900 

Boardman $339,900 

Boardman $339,900 

Boardman $339,900 

Boardman $339,900 

Boardman $339,900 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

JUST move in! this home has over 3000 sq-ft; $339,900. 

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

Cocca Real Estate 330-758-9904  

330-758-9904

330-758-9904

330-758-9904

330-758-9904

330-758-9904

330-758-9904

330-758-9904

====
====
====
====
====
====
====

Thanks for all your help!
 
Code:
#!/usr/bin/perl
$/="\n";


my @split_strings;
my @first_half;
my @second_half;

open (READFROM, "config.txt") or die "Cannot open $readfrom: $!";
while ( <READFROM> ) 
{ 
  chomp;
  push(@split_strings,$_);
}

close (READFROM) or die "Error: $!";
my $sep = qr/\Q||\E/;

foreach $line (@split_strings) { 
  my ($first,$second) = $line =~ /^(.+)\s+$sep\s+(.+)$/; 
  push @first_half, $first; 
  push @second_half, $second; 
}

foreach (@first_half) {
  print "$_\n"; 
}
print "\n";
foreach (@second_half) { 
   print "$_\n";
} 
open (SOURCE, "<aa1.txt") or die "Cannot open $sourcefile: $!";
open (WRITETO, ">aa2.txt") or die "Cannot open $writeto: $!";
while (<SOURCE>) {
  $line=$_;
  foreach ( @first_half ) {
     $line =~ s/$first_half[$_]/$second_half[$_]/;
  }
  print WRITETO "$line\n"; # move this line outside the loop

}
close (SOURCE) or die "Error occured for $sourcefile: $!";
close (WRITETO) or die "Error occured for $writeto: $!";
HTH
--Paul
 
Thanks. I ran this code but it's not s/// anything from my file. All it seems to do is print the original to the output.
 
Right, one more thing

/gi modifiers on the substitution to do a global replacements, and to ignore case

Code:
#!/usr/bin/perl
$/="\n";


my @split_strings;
my @first_half;
my @second_half;

open (READFROM, "config.txt") or die "Cannot open $readfrom: $!";
while ( <READFROM> ) 
{ 
  chomp;
  push(@split_strings,$_);
}

close (READFROM) or die "Error: $!";
my $sep = qr/\Q||\E/;

foreach $line (@split_strings) { 
  my ($first,$second) = $line =~ /^(.+)\s+$sep\s+(.+)$/; 
  push @first_half, $first; 
  push @second_half, $second; 
}

foreach (@first_half) {
  print "$_\n"; 
}
print "\n";
foreach (@second_half) { 
   print "$_\n";
} 
open (SOURCE, "<aa1.txt") or die "Cannot open $sourcefile: $!";
open (WRITETO, ">aa2.txt") or die "Cannot open $writeto: $!";
while (<SOURCE>) {
  $line=$_;
  foreach ( @first_half ) {
     $line =~ s/$first_half[$_]/$second_half[$_]/[COLOR=red]ig[/color];
  }
  print WRITETO "$line\n";

}
close (SOURCE) or die "Error occured for $sourcefile: $!";
close (WRITETO) or die "Error occured for $writeto: $!";

This works for me
--Paul
 
Hiya Paul.

I saw that after I posted this and it didn't replace anything this time around either. This seems like to be one of those projects you start but can't quite finish due to problems, it'd be the third script this month where something was always holding it back.

This one gets filed now, lol, thanks for all your help!
 
I believe I am but I bet it's a simple over looked issue. Here is what I have so at this time.

Code:
replace.txt ($readfrom)
bdroom || bedroom
clst || closet
on || xxxxxxxxxxxxxxxxx
this string || ---
bowl of || plate of
apple || pear
php || ---
love || hate
You'll || xxxxxxxxxxxxxxxxxxxx

Code:
test.txt ($sourcefile)
You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 
====

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 
====

Boardman $157,000 
COLONIAL/1 ACRE 
WOW! 
We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 
David Realty 330-758-8363  
330-758-8363
====

Boardman  $475 
A BARGAIN - 0 DOWN 
$475 & up/mo. 
2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 
7372 Oregon Trail 
7421 Siera Madre 
All Credit Considered 
Jim Rich Realty 330-783-9300 
====

North Jackson  
JACKSON MEADOWS 
Custom built 3&4 bedroom homes 
330-538-4663 or 330-503-1985  
330-538-4663
====

Dogs  
PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 
====

Austintown  
STANJIM HOMES 
1850 Countryside Drive 
Kingwood Real Estate 793-2010  
793-2010
====

Boardman $339,900 
JUST move in! this home has over 3000 sq-ft; $339,900. 
Cocca Real Estate 330-758-9904  
330-758-9904
====

Code:
output.txt ($writeto)
You’ll love this Beautiful Cape Cod on the Boardman side of Southern Blvd. We can do a Rent-to-Own for a short time if you can qualify quickly for this 2-bedroom home wtih all appliances, full basement and garage, with payments as easy as rent. We have only one home at these terms, so call 330-207-0989 NOW - it won’t last long! 

====



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 

====



Boardman $157,000 

COLONIAL/1 ACRE 

WOW! 

We finally found the 4 bedroom home you’ve been looking for. Call our office today for details and directions for the best buy in Boardman. Appraised at $157,000. Asking ONLY $139,900 

David Realty 330-758-8363  

330-758-8363

====



Boardman  $475 

A BARGAIN - 0 DOWN 

$475 & up/mo. 

2 Bedroom, extra sharp ranch. Ready to occupy, not for rent. Easy to purchase. 2 homes available. 

7372 Oregon Trail 

7421 Siera Madre 

All Credit Considered 

Jim Rich Realty 330-783-9300 

====



North Jackson  

JACKSON MEADOWS 

Custom built 3&4 bedroom homes 

330-538-4663 or 330-503-1985  

330-538-4663

====



Dogs  

PLEASE HELP! Being relocated-looking for a good home for my best friend 2 yr old blonde Cocker Spaniel well trained Loves to play Frisbee & Squeezy ball 330-797-8724 

====



Austintown  

STANJIM HOMES 

1850 Countryside Drive 

Kingwood Real Estate 793-2010  

793-2010

====



Boardman $339,900 

JUST move in! this home has over 3000 sq-ft; $339,900. 

Cocca Real Estate 330-758-9904  

330-758-9904

====


Every time I run the script the output is an exact copy of the original for some reason (with a few new lines, but that's not a problem).

My script is what you had exactly:

Code:
#!/usr/bin/perl
$/="\n";


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

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

my $sourcefile = "test.txt";
## change the above to the source file you're scanning

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

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


my @split_strings;
my @first_half;
my @second_half;

open (READFROM, "$readfrom") or die "Cannot open $readfrom: $!";
while ( <READFROM> ) 
{ 
  chomp;
  push(@split_strings,$_);
}

close (READFROM) or die "Error: $!";
my $sep = qr/\Q||\E/;

foreach $line (@split_strings) { 
  my ($first,$second) = $line =~ /^(.+)\s+$sep\s+(.+)$/; 
  push @first_half, $first; 
  push @second_half, $second; 
}

foreach (@first_half) {
  print "$_\n"; 
}
print "\n";
foreach (@second_half) { 
   print "$_\n";
} 
open (SOURCE, "< $sourcefile") or die "Cannot open $sourcefile: $!";
open (WRITETO, "> $writeto") or die "Cannot open $writeto: $!";
while (<SOURCE>) {
  $line=$_;
  foreach ( @first_half ) {
     $line =~ s/$first_half[$_]/$second_half[$_]/ig;
  }
  print WRITETO "$line\n"; # move this line outside the loop

}
close (SOURCE) or die "Error occured for $sourcefile: $!";
close (WRITETO) or die "Error occured for $writeto: $!";


Which doesn't make any sense because everything is ready properly, the array prints are working and the output is writing.

I don't have an account there, should I get one?

Thanks!
 
Code:
#!/usr/bin/perl
$/="\n";


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

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

my $sourcefile = "infile.txt";
## change the above to the source file you're scanning

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

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


my @split_strings;
my @first_half;
my @second_half;

open (READFROM, "$readfrom") or die "Cannot open $readfrom: $!";
while ( <READFROM> ) 
{ 
  chomp;
  push(@split_strings,$_);
}

close (READFROM) or die "Error: $!";
my $sep = qr/\Q||\E/;

foreach $line (@split_strings) { 
  my ($first,$second) = $line =~ /^(.+)\s+$sep\s+(.+)$/; 
  push @first_half, $first; 
  push @second_half, $second; 
}
$i=0;
foreach (@first_half) {
  print "$first_half[$i]  --  $second_half[$i]\n";
  $i++;
}
print "\n";
open (SOURCE, "< $sourcefile") or die "Cannot open $sourcefile: $!";
open (WRITETO, "> $writeto") or die "Cannot open $writeto: $!";
while (<SOURCE>) {
  $line=$_;
  [COLOR=red]$i=0;[/color]
  foreach ( @first_half ) {
     $line =~ s/$first_half[[COLOR=red]$i[/color]]/$second_half[[COLOR=red]$i[/color]]/ig;
[COLOR=green]     # $_ in the line above is being overwritten by the values in the regex, 
     # which might, but most likely don't have a corresponding value in the array [/color]
     [COLOR=red]$i++;  [/color]
  }
  print WRITETO "$line\n"; # move this line outside the loop

}
close (SOURCE) or die "Error occured for $sourcefile: $!";
close (WRITETO) or die "Error occured for $writeto: $!";

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Hey thanks Paul, I really appreciate all your help. It is up and running now and it's all thanks to you.

This really isn't my type of programming, I prefer playing in CGI with databases and forms and ImageMagick and fun things like that. Text parsing/manipulation isn't one of my strong points, neither are regexes. Though I believe I'm getting better at regexes, everyone gives me "another" way to do it so I'm not really sure :)

I want to get a book on Regexes but I can't justify the money for one at the moment. I am trying to learn PerlTK. Not sure if you know that module or not, it's really neat.

Thanks again for all your help, I will read this time and time again until I come to realize why my way of things didn't work.

Star for you!

spydermonkey
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top