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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me replace string in a file with contents of array please!

Status
Not open for further replies.

Jimbo2112

IS-IT--Management
Mar 18, 2002
109
GB
Hi All,

I have a file called $topics_final with contents that look like this:

329/7461/301 129
329/7461/302 159
329/7461/303 116
329/7461/304 258
329/7461/305 154
329/7461/305 200
{NEWS HERE}
329/7461/311 202
329/7461/311 155
329/7461/315 47
329/7461/315 218
329/7461/315 137
329/7461/315 299
329/7461/318 15
329/7461/318 106
329/7461/322 60
329/7461/322 116
329/7461/323 328
329/7461/323 76

I also have an array called @news_topics with contents that look like this:

329/7461/1091 96
329/7461/1092 25
329/7461/1092 111
329/7461/1092 218
329/7461/1092 117
329/7461/1092 50
329/7461/1092 201
329/7461/1093 36
329/7461/1093 201
329/7461/1094 214
329/7461/1094 365
329/7461/1094 11
329/7461/1094 116
329/7461/1094 274
329/7461/1094 365

I need my script to open the file and replace the string:
{NEWS_HERE}
with the second batch of code above. Here is the subroutine I have got around this task:

#---------------------------------------------------------------------------------#

sub MERGE_DATA
{
$news = $holding_area."news";
$news_done = $news."_done";
open INPUT, "$news" or die "\n\n\n Could not open $news";
open OUTPUT, ">$news_done" or die "\n\n\n Couldn't open $news_done";
while (<INPUT>)
{
$_=~ s|//|/$issue/|;
print OUTPUT $_;
}
close(INPUT);
close(OUTPUT);

open(TOPICS, $topic_final) or die "\n\n\n Could not open topics";
@news_topics=<TOPICS>;
close(BMJ_TOPICS);

open FINAL, "$topics_final" or die "\n Cannot open $topics_final";
s/^\{NEWS_HERE\}$/@news_topics/ or print "\n\n\n NOT FOUND";
close(FINAL);
}
#---------------------------------------------------------------------------------#

This is not doing the replace as I had wished. Can anyone give me a pointer as to what code I need to get the {NEWS_HERE} string to be replaced by the contents of the array?

Many thanks

Jimbo
 
Try this:

Code:
sub MERGE_DATA
{
    $news = $holding_area."news";
    $news_done = $news."_done";
    open INPUT, "$news" or die "\n\n\n     Could not open $news";
    open OUTPUT, ">$news_done" or die "\n\n\n     Couldn't open $news_done";
    while (<INPUT>)
    {
        $_=~ s|//|/$issue/|;
        print OUTPUT $_;
    }
    close(INPUT);
    close(OUTPUT);
    
    open(TOPICS, $topic_final) or die "\n\n\n     Could not open topics";
    open FINAL, "$topics_final" or die "\n   Cannot open $topics_final";
    while ($line = <TOPIC>) # Read each line of the file TOPIC
    {
        chomp $line; # Kill carraige returns
        if ($line =~ /{NEWS_HERE}/) # if line = {NEWS_HERE}
        {
             # Print each element from News file
             foreach $ele (@news_topics)
             {
                 print FINAL qq($ele\n);
             }
         }
         # Otherwise, print the line to the final file
         else
         {
              print FINAL qq($line\n);
         }
     }

    close(TOPICS);
    close(FINAL);
}

- Rieekan
 
Code:
#!perl
use strict;
use warnings;

open INPUT, "news.txt" or die "\n\n\n     Could not open news";
open(TOPICS, "topics.txt") or die "\n\n\n     Could not open topics";
my @news_topics=<TOPICS>; 
close(TOPICS);
open OUTPUT, ">final.txt" or die "\n\n\n     Couldn't open final";
while (<INPUT>)
{
    if (/^{NEWS HERE}$/) {
        for my $topic (@news_topics) {
            print OUTPUT $topic;
        }
    } else {
        print OUTPUT $_;
    }
}
close(INPUT);
close(OUTPUT);
Comments:
0. I changed your file names.
1. $_=~ s|//|/$issue/|;: What's this about? There's no such string in your input, nor is there a variable called $issue.
2. open(TOPICS, $topic_final): No variable $topic_final.
3. close(BMJ_TOPICS);: Clearly you meant TOPICS, as there's no such file handle as BMJ_TOPICS.
4. Use strict and declare all your variables with my.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top