Solved

Misc Questions

Posted on 2002-04-16
24
206 Views
Last Modified: 2010-03-05
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
Comment
Question by:tel2
  • 10
  • 8
  • 4
  • +1
24 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6946847
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
 
LVL 12

Author Comment

by:tel2
ID: 6946867
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6947223
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Expert Comment

by:maneshr
ID: 6948973
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
 
LVL 12

Author Comment

by:tel2
ID: 6949132
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
 
LVL 12

Author Comment

by:tel2
ID: 6958353
Hello?
0
 
LVL 84

Expert Comment

by:ozo
ID: 6958426
{local @ARGV=("file1","file2");
  while( <> ){
    #"process-the-data"
  }
}
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6962125
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
 
LVL 12

Author Comment

by:tel2
ID: 6964681
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6964902
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
 
LVL 12

Author Comment

by:tel2
ID: 6970015
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970032
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
 
LVL 84

Expert Comment

by:ozo
ID: 6970822
2.
while( $_ = <INFILE> || ("END-OF-FILE\n")[~(0..1)] ){
    # process-the-data  
}
0
 
LVL 12

Author Comment

by:tel2
ID: 6976166
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
 
LVL 12

Author Comment

by:tel2
ID: 6976170
ozo,

Correction: Apart from that "1" thing, it looks promising.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6976496
what does file2 contain?
0
 
LVL 12

Author Comment

by:tel2
ID: 6978877
ahoffmann,

file2 is only relevant to question 1.
Question 2 uses file1, and a hard coded trailer record.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6979818
2. while(<INFILE>){print "Rec = $_";}
   print "Rec = d";
0
 
LVL 12

Author Comment

by:tel2
ID: 6982246
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6985242
open(INPUT,"file1");
$eof=1;
while($eof){
  $_=<INPUT>;
  if (! $_){$eof=0;};
  # process-data-here
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6988143
open(INFILE,"file1");
while( $_ = <INFILE> || ($.=0,("END-OF-FILE\n")[-(0..1)])  ){
   print "Rec = $_";
}
0
 
LVL 12

Author Comment

by:tel2
ID: 7012917
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7014624
grading is ok for me ;-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 7028489
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question