I was reading this thread and I didn't see any definition of the input files until Re^4: Need to replace string from external file! In the future, please start with a very clear example of your code and the input files and the desired output.

It is possible to make a single Perl program file that contains multiple input files for reading. I show an example below. In your case, there are 3 input files and the output goes to STDOUT. Anybody can download my single .pl file and run it and see what it does.

You seem to be having trouble dealing with files in general. How to open a file handle, etc. The Monks have given you some great pointers on that. This is a basic thing that you need to know how to do in any programming language.

I work with beginners in a number of programming languages at a local college. The first and most important file skill (besides how to open and close files) is to process input files one line at a time. When you create an array of the entire file in memory (slurp, etc) and then process that array, you are actually reading each data line twice and consuming memory in the process. This is usually not necessary or desired.

Your desired code does seem a bit contrived to me. Is this a homework assignment? I would not have written an assignment like this. Having 2 files where line 3 of one matches up with line 3 of the other is pretty rare. That is because this is a very error prone format - there is a lot that can go wrong! But yes, these things do exist.

Below I check for "defined" when reading a line from the inline file. That is because EOF (End of File) for an inline file is detected a bit differently than when reading an actual file. This extra (is defined?) step works fine with real files, but isn't necessary.

#!/usr/bin/perl use strict; use warnings; use Inline::Files; # allows files to be included in source code my $template = <TEMPLATE>; # reads first line # $template has a trailing \n my $name; while (defined ($name = <NAME>) and $name =~ /\S+/) # process until bl +ank line { chomp $name; my $ip; defined ($ip = <IP>) and $ip =~ /\S+/ or die "Mis-matched number o +f lines"; chomp $ip; printf ($template,$name,$ip); # note uses the \n within template +line } =prints: my name is john. My local machine ip is 10.0.1.2. my name is peter. My local machine ip is 10.2.3.4. my name is mickey. My local machine ip is 10.3.4.5. =cut __TEMPLATE__ my name is %s. My local machine ip is %s. __NAME__ john peter mickey __IP__ 10.0.1.2 10.2.3.4 10.3.4.5
Update:
This inline file stuff can be a good idea.
I wrote some code last week with 4 inline files, 2 input files and 2 output files. This code is now in production - this has real uses, not just theory. I include a file into the source code when I expect that the person who modifies this file later will need to understand the actual source code. This "file" is in the source code because a "programmer" is needed to modify it. That is a different thing than a configuration file which is intended to be modified by average users of the S/W.

In reply to Re: Need to replace string from external file by Marshall
in thread Need to replace string from external file by bhupi70007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.