• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Misc Questions

4 questions:


1. Merging input from multiple input files
I would like to take 2 or more files as input to one "while" loop.  Below is an example with one file:
  open(INFILE,"<file1");
  while(<INFILE>)
  { process-the-data }
How might I process "file1" and "file2" (for example), without having multiple while loops?  If possible, I'd like to AVOID creating a temporary file (the 2 files combined).


2. Appending trailer record to input stream
Similar to question 1, but I just want one "trailer" record added to end of the input stream.  That record might be:
  "END-OF-FILE"
for example, and it's not stored in a file (it's a literal in the Perl script).

The above 2 questions relate to Perl 5 on AIX 4.3.2.


3. Perl 4 subroutines.
In Perl 4, how do you code a subroutine?  I've tried the Perl 5 syntax, but I get an error, eg, this:
  test1();
  sub test1
  { print "Test\n"; }
Gives this error:
  syntax error in file ./test at line 3, next 2 tokens "test1("
  Execution of ./test aborted due to compilation errors.

The above question relates to Perl 4.0.1.7 on AIX 3.2.5, and I haven't yet found Perl 5 binaries for this (old) version of AIX.


4. Where can I get Perl 5 binaries for AIX 3.2.5?
0
tel2
Asked:
tel2
  • 10
  • 8
  • 4
  • +1
1 Solution
 
maneshrCommented:
tel2,

Welcome back!!

You have the following 3 questions open for a long time now. Please take some to review & close these questions.

EE userid tel2
Total questions asked 45 (100%)
Open questions 3
       
Topic Area              URL              Date              
Unix   http://www.experts-exchange.com/jsp/qShow.jsp?ta=unix&qid=11983719   11/27/00  
Unix  Setup  http://www.experts-exchange.com/jsp/qShow.jsp?ta=unixsetup&qid=11894198 11/16/2000
Unix   http://www.experts-exchange.com/jsp/qShow.jsp?ta=unix&qid=20243711   12/04/01  

Your help in closing these questions will be highly appreciated.

Thanks,

maneshr
(NOT a moderator at EE)
0
 
tel2Author Commented:
Fair point, maneshr!

As a result of your reminder, I've deleted one (which was a link to another, which I've now assigned points for), and I've attempted to delete the 3rd, for which the answer didn't really help.

Sorry for my slackness.
0
 
ahoffmannCommented:
1.
open(F1,"file1");
open(F2,"file2");
while( whatever-you-like) {
 $l1=<F1>;
 $l2=<F2>;
}

2. if(! $l1=<F1>)){$l1="END-OF-FILE";}

3. &test1();

4. http://www.cpan.org/ports/index.html#aix
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
maneshrCommented:
tel2,

"..I've deleted one (which was a link to another, which I've now assigned
points for), and I've attempted to delete the 3rd, for which the answer didn't really help.

Sorry for my slackness. .."

Hey, No problem!!. Thanks for taking the time to close these questions.

Rgds,
0
 
tel2Author Commented:
ahoffmann,

Thanks for your answers so far.

1. I'm sorry, but I don't understand how I can use your answer.  I want to perform "process-the-data" for each line of file1, and then for each line of file2, within one while loop.  Please give me the syntax with the "process-the-data" step included.
I also don't know what you mean by "whatever-you-like", as the while condition.  What I like is: while "there's-still-data-to-process".

2. I don't understand how I can use this either.  Please give me an example in the form of the while syntax listed in question 1, including the "process-the-data" step, which should be executed once for each record, including the trailer record.

3. EXCELLENT!  I have no help files installed on the Perl 4 machine, so this is good to know.  Is there somewhere I can find Perl 4 help on the web?

4. I had been to this page previously, and had a quick look, and I can see Perl for AIX 4, etc, but not for AIX 3.2.5.
0
 
tel2Author Commented:
Hello?
0
 
ozoCommented:
{local @ARGV=("file1","file2");
  while( <> ){
    #"process-the-data"
  }
}
0
 
ahoffmannCommented:
1. see ozo's sugestion (it's more perlish than mine:)
     also substitute "whatever-you-like" by "there's-still-data-to-process".

2. if *you* don't understand, *I* do either
   could you please give a detailed example what you wnat to do

3. STFW - search the fine web :-)

4. I'm out of ideas, sorry
0
 
tel2Author Commented:
ahoffmann,

1. OK.  I'll try it some time.

2. Sure.  Here's the loop:
   open(INFILE,"<file1");
   while(<INFILE>)
   { process-the-data }
where file1 might contain data like:
   a
   b
   c
but I want a "trailer" record (eg: "d", which is not in file1) to be processed, as part of the loop, after the "c" record is processed, without actually appending it onto file1, but just appending it onto the end of the input stream.  This saves me from having to repeat all the "process-the-data" code, after the end of the loop, (and saves me from having to make it into a subroutine).

3. "Search the web" is a fairly obvious and unhelpful answer to the question "Is there somewhere I can find Perl 4 help on the web?".  If you don't know, then just say you don't know, and if no one else happens to know a good source of Perl 4 help on the web, then I might take the time to search it myself.

4. No problem.  I might have to get source and compile it.
0
 
ahoffmannCommented:
2. still do not understand what you want to accomplish
   If the while(<INFILE>) loop terminates, you're just at the point where there is no more in put, then you can process anything else.
   What do you need in the INPUT-stream if there is nothing to process?

3. hmm, I gave a somehow sarcastic answer, sorry.
   You need to install the perl doc files from the distribution, then "perldoc -q somthing" or "man perl" will give the answer, somewhere, somehow, I'm shure.

4. .. and install it using "make install", then 3. is solved too :-)
0
 
tel2Author Commented:
ahoffmann,

Thanks for your reply.

2. I want to simulate having an extra record at the end of the input file, without actually changing the the input file itself.
Having this trailer record in the input stream, makes the coding simpler, because I don't have to repeat any code after end-of-file (even though it could be just subroutine calls), to handle the last control break (which would otherwise have been end-of-file, but will now just be another control break).
Let me know if you need further explanation.

3. The Perl 4 "installation" is not complete (it consists the "perl" executable only), so unfortunately I can't use perldoc in this case.

4. OK - thanks.
0
 
ahoffmannCommented:
2. ok, think I got it:
   replace: while(<INFILE>){ process-the-data }
   by: $eof=1;while($eof){if (<INPUT>){$_=<INPUT>;}else{$eof=0;} process-data-depending-on-$eof }
   # to be improved in many ways ...
0
 
ozoCommented:
2.
while( $_ = <INFILE> || ("END-OF-FILE\n")[~(0..1)] ){
    # process-the-data  
}
0
 
tel2Author Commented:
Sorry for the delay in getting back to y'all on this.

Let's assume file1 contains the data:
    a
    b
    c
And we want a "trailer" record of "d" to be appended to the input stream.  So, if the process-data code was simplified to 'print "Rec = $_\n";', I'd expect this output:
    Rec = a
    Rec = b
    Rec = c
    Rec = d


ahoffmann,

I'm not sure what the "process-data-depending-on-$eof" is for, but here's your script with an open and print added:
    open(INPUT,"file1");
$eof=1;while($eof){if (<INPUT>){$_=<INPUT>;}else{$eof=0;}
    print "Rec = $_\n";}

The output I get from this is:
    Rec = b

    Rec =
    Rec =

Any ideas what's wrong?



ozo,

Here's your script with an open and print added:
    open(INFILE,"file1");
    while ($_ = <INFILE> || ("END-OF-FILE\n")[~(0..1)] )
    { print "Rec = $_"; }

And this gives the following output:
    Rec = a
    Rec = b
    Rec = c
    Rec = END-OF-FILE
    Rec = END-OF-FILE
    ...etc...
The "END-OF-FILE"s repeat infinitely.  Apart from those 2 things, this looks promising.
What's the "[~(0..1)]" mean?
Any ideas what's wrong?


Thanks.
0
 
tel2Author Commented:
ozo,

Correction: Apart from that "1" thing, it looks promising.
0
 
ahoffmannCommented:
what does file2 contain?
0
 
tel2Author Commented:
ahoffmann,

file2 is only relevant to question 1.
Question 2 uses file1, and a hard coded trailer record.
0
 
ahoffmannCommented:
2. while(<INFILE>){print "Rec = $_";}
   print "Rec = d";
0
 
tel2Author Commented:
ahoffmann,

Thanks for your response.

Yes, that's how I've always done it in the past, but I'm trying to find another way, which doesn't require repetition of code (or even repetition of subroutine calls).  That's why I think I need to append to the input stream.  Your answer looks quite tidy when "process-data" is just a "print" (which was my "simplified" (as mentioned on 04/28/2002) example), but when it's 20 lines of code, you either have to make a subroutine (which I'm also trying to avoid here), or repeat 20 lines of code.  See my comments re question 2 on 04/23/2002.

I think ozo's answer is almost what I need.
0
 
ahoffmannCommented:
open(INPUT,"file1");
$eof=1;
while($eof){
  $_=<INPUT>;
  if (! $_){$eof=0;};
  # process-data-here
}
0
 
ozoCommented:
open(INFILE,"file1");
while( $_ = <INFILE> || ($.=0,("END-OF-FILE\n")[-(0..1)])  ){
   print "Rec = $_";
}
0
 
tel2Author Commented:
Sorry for the delay in getting back to you guys.


ozo,

1. The output from your answer seems to be correct.
Does the use of "local", free up memory after the last "}", or what?

2. The output of your code for this question is:
    Rec = a
    Rec = b
    Rec = c
    Rec = END-OF-FILE
Which is what I wanted - thanks.


ahoffmann,

2. To test your code, I changed the '# process-data-here' line to 'print "Rec = $_";'.  I then get this output:
    Rec = a
    Rec = b
    Rec = c
    Rec =
Note that there is no trailer record (like "d" or "END-OF-FILE") printing.

3. Thanks for your answer to this question.


ozo & ahoffmann,

I was going to give the points to ahoffmann, since he has probably put more time into this, but since ozo answered 2 questions (thanks ozo), and ahoffmann answered 1, I'd better give them to ozo.  I've awarded a B since ozo answered only half of the questions.
Thanks for your efforts, ahoffmann.
0
 
ahoffmannCommented:
grading is ok for me ;-)
0
 
ozoCommented:
I'd have preferred to split 1/2 an A with ahoffmann  

{local @ARGV} protects from interfering with any use of @ARGV elsewhere in your program

($.=0,("END-OF-FILE\n")[-(0..1)]) might have been better written like
("END-OF-FILE\n")[-($.||1..$.&&0)]
You can also add other lines in the ()

("penultimate-of-file\n","END-OF-FILE\n")[($.||1..$.&&0)-1]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 10
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now