Solved

Misc Questions

Posted on 2002-04-16
24
200 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
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
Hello?
0
 
LVL 84

Expert Comment

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

Expert Comment

by:ahoffmann
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

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

Author Comment

by:tel2
Comment Utility
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 11

Author Comment

by:tel2
Comment Utility
ozo,

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

Expert Comment

by:ahoffmann
Comment Utility
what does file2 contain?
0
 
LVL 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
2. while(<INFILE>){print "Rec = $_";}
   print "Rec = d";
0
 
LVL 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
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
Comment Utility
open(INFILE,"file1");
while( $_ = <INFILE> || ($.=0,("END-OF-FILE\n")[-(0..1)])  ){
   print "Rec = $_";
}
0
 
LVL 11

Author Comment

by:tel2
Comment Utility
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
Comment Utility
grading is ok for me ;-)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now