Opening two files at once

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Opening two files at once

    I have two text files I would like to open and read at the same time.
    Whats the best way to do this?

    I was think along these lines:

    open (TESTFILE, <"somefile.txt")|| die "can not open file";
    open (LOGFILE, <"my_other_file")|| die "can not open other file";
    while (($line1 = <TESTFILE>) & ($line2 = <LOGFILE>))
    {
    # print the line from each file
    print "$line1\n $line2\n";
    }
    close TESTFILE;
    close LOGFILE;


    I know the while statement is wrong,

    Whats the best way, and most understandable way?
    and is there a way to do this without loading a module?

    Thanks

    Denis

    denis@teachlinux.com Guest

  2. Similar Questions and Discussions

    1. #39841 [NEW]: fopen is not opening external urls but opening local files
      From: sarvesh_borkar at yahoo dot co dot in Operating system: FreeBSD PHP version: 5.2.0 PHP Bug Type: Filesystem function...
    2. Opening FDF Files using pdf.ocx
      Hi Is there a way i can open an FDF File using the pdf.ocx, i've tried it using the loadfile method but i'm getting an error saying "File does not...
    3. Opening FH5 files in OSX
      I am having problems opening some OLD Freehand 5 files in Freehand MX using OSX. Does anyone have any tips ( apart from booting in classic and...
    4. Opening DIR files
      Hi all, I think I have a fairly simple question that seems to have illuded me for quite some time now. See, the problem is that when I am using...
    5. opening CHM files?
      In article <210720031832571970%blah@blah.com>, blah blah <blah@blah.com> wrote: It's a bit of a pain but you can convert them in the terminal,...
  3. #2

    Default RE: Opening two files at once

    [email]denis@teachlinux.com[/email] <enis@teachlinux.com> wrote:
    :
    : I have two text files I would like to open and read at the
    : same time. Whats the best way to do this?
    :
    : I was think along these lines:
    :
    : open (TESTFILE, "<somefile.txt")|| die "can not open file";

    It is helpful to add the perl error code in there:

    open (TESTFILE, "<somefile.txt") ||
    die qq|Cannot open "somefile.txt": $!";

    : open (LOGFILE, "<my_other_file")|| die "can not open other file";

    It would be helpful to know what you're going to do
    with the files. Do they need to be synchronized? That is,
    do you need to access the same line of each file together
    or do you need to access lines of one file and different
    lines of the other?

    Are they small files? Can they be safely loaded into
    memory or are they going to be large files? Do you ever
    need to go back to an old line you have already processed?
    Will there ever be a need for more than three files?
    Finally, are you using perl 5.6.1 or above?

    You're going to get some helpful answers, but more
    information about what you intend to do with the files
    can aid us in finding better solutions.


    HTH,

    Charles K. Clarkson
    --
    Head Bottle Washer,
    Clarkson Energy Homes, Inc.
    Mobile Home Specialists
    254 968-8328








    Charles K. Clarkson Guest

  4. #3

    Default Re: Opening two files at once


    On Dec 22, 2003, at 4:32 PM, [email]denis@teachlinux.com[/email] wrote:
    > I have two text files I would like to open and read at the same time.
    > Whats the best way to do this?
    the only problem with your current form, barring
    the faux paux of

    while( ($line1 = <TESTFILE>) && ($line2 = <LOGFILE>))
    {

    }

    is what to do with the case of one file being longer than
    the other file.

    So you might try the idea of

    my ($line1, $line2 , $flag );
    while ( 1 ) {
    $line1 = <TESTFILE>; # read them each
    $line2 = <LOGFILE>;
    last unless ( $line1 || $line2 ); # if both empty exit loop
    #
    # otherwise test for which one just finished
    #
    unless( $line1 )
    {
    $flag = 1;
    last;
    }
    unless( $line2 )
    {
    $flag = 2;
    last
    }
    #
    # now do the voodo on the two lines
    #
    chomp($line2);
    chomp($line1);
    print "$line2 : $line1\n";
    }
    # if one needs to know which ended first one
    # can deal with that here...
    if ( $flag )
    {
    print "more lines to read\n";
    print "\tfrom TestFile \n" if ( $flag == 1 );
    print "\tfrom logfile \n" if ( $flag == 2 );
    } else {
    print "both ended together\n";
    }
    # now one would be safe to close the files

    HTH.

    ciao
    drieux

    ---

    Drieux Guest

  5. #4

    Default RE: Opening two files at once

    On Mon, 22 Dec 2003, Charles K. Clarkson wrote:
    > [email]denis@teachlinux.com[/email] <enis@teachlinux.com> wrote:
    > :
    > : I have two text files I would like to open and read at the
    > : same time. Whats the best way to do this?
    > :
    > : I was think along these lines:
    > :
    > : open (TESTFILE, "<somefile.txt")|| die "can not open file";
    >
    > It is helpful to add the perl error code in there:
    >
    > open (TESTFILE, "<somefile.txt") ||
    > die qq|Cannot open "somefile.txt": $!";
    >
    > : open (LOGFILE, "<my_other_file")|| die "can not open other file";
    >
    > It would be helpful to know what you're going to do
    > with the files. Do they need to be synchronized? That is,
    > do you need to access the same line of each file together
    > or do you need to access lines of one file and different
    > lines of the other?
    >
    > Are they small files? Can they be safely loaded into
    > memory or are they going to be large files? Do you ever
    > need to go back to an old line you have already processed?
    > Will there ever be a need for more than three files?
    > Finally, are you using perl 5.6.1 or above?
    >
    > You're going to get some helpful answers, but more
    > information about what you intend to do with the files
    > can aid us in finding better solutions.
    >
    >
    > HTH,
    >
    > Charles K. Clarkson
    >

    Thanks Charles,

    The files are smaller then 1000 lines, so dropping thme into a memory
    array would work.
    Dont see ever haveing to go back and re-read them, other then re-running
    the script. Would have to access the same line in each file at the same
    time.

    Runing Perl 5.8 on most systems, there are some systems that I'm not sure,
    but will assume that there running at least 5.6.1

    Will need to ensure that each file has the same number of lines included.
    Was thinking of doing this via the "wc" command in unix.

    The thing that really stinks is not being able to load modules on the
    remote systems.

    I did hack some code out last night that looks like this:

    #!/usr/bin/perl -w
    my @test1;
    my @test2;
    my $x = 0;

    open (TEST1, "</etc/passwd")|| die " cant open passwd\n";
    open (TEST2, "</etc/group")|| die " cant open group\n";
    @test1 = <TEST1>;
    @test2 = <TEST2>;
    for ($x = 0; $x != $#test1; $x++)
    {
    print ($test1[$x]);
    print ($test2[$x]);
    print "\n";
    }

    there's not any line count check.. figured I'd use something like this..

    # make sure the files have the same number of lines
    @line_count = (`wc -l *tcp* *udp*`);
    ($lc, @rem) = split(" ",$line_count[0]);
    for (@line_count)
    {
    ($lc1_temp, @rem) = split (" ");
    }
    if ($lc1_temp/$#line_count != $lc)
    {
    die "\n\n\n Files do not have the same number of lines!\n\n\n";
    }

    Thanks,

    Denis

    denis@teachlinux.com Guest

  6. #5

    Default Re: Opening two files at once

    On Mon, 22 Dec 2003, drieux wrote:
    >
    > On Dec 22, 2003, at 4:32 PM, [email]denis@teachlinux.com[/email] wrote:
    >
    > > I have two text files I would like to open and read at the same time.
    > > Whats the best way to do this?
    >
    > the only problem with your current form, barring
    > the faux paux of
    >
    > while( ($line1 = <TESTFILE>) && ($line2 = <LOGFILE>))
    > {
    >
    > }
    >
    > is what to do with the case of one file being longer than
    > the other file.
    >
    > So you might try the idea of
    >
    > my ($line1, $line2 , $flag );
    > while ( 1 ) {
    > $line1 = <TESTFILE>; # read them each
    > $line2 = <LOGFILE>;
    > last unless ( $line1 || $line2 ); # if both empty exit loop
    > #
    > # otherwise test for which one just finished
    > #
    > unless( $line1 )
    > {
    > $flag = 1;
    > last;
    > }
    > unless( $line2 )
    > {
    > $flag = 2;
    > last
    > }
    > #
    > # now do the voodo on the two lines
    > #
    > chomp($line2);
    > chomp($line1);
    > print "$line2 : $line1\n";
    > }
    > # if one needs to know which ended first one
    > # can deal with that here...
    > if ( $flag )
    > {
    > print "more lines to read\n";
    > print "\tfrom TestFile \n" if ( $flag == 1 );
    > print "\tfrom logfile \n" if ( $flag == 2 );
    > } else {
    > print "both ended together\n";
    > }
    > # now one would be safe to close the files
    >
    > HTH.
    >
    > ciao
    > drieux
    >
    > ---
    >
    >
    >
    Thanks, looks pretty good..

    denis@teachlinux.com Guest

  7. #6

    Default Re: Opening two files at once

    Drieux,

    Sorry for the curt thank you. You code looks really good.

    On Mon, 22 Dec 2003, drieux wrote:
    >
    > On Dec 22, 2003, at 4:32 PM, [email]denis@teachlinux.com[/email] wrote:
    >
    > > I have two text files I would like to open and read at the same time.
    > > Whats the best way to do this?
    >
    > the only problem with your current form, barring
    > the faux paux of
    >
    > while( ($line1 = <TESTFILE>) && ($line2 = <LOGFILE>))
    > {
    >
    > }
    >
    > is what to do with the case of one file being longer than
    > the other file.
    >
    > So you might try the idea of
    >
    > my ($line1, $line2 , $flag );
    > while ( 1 ) {
    > $line1 = <TESTFILE>; # read them each
    > $line2 = <LOGFILE>;
    never thought of reading two files one line at a time like this, looks so
    easy once you showed me.
    > last unless ( $line1 || $line2 ); # if both empty exit loop
    > #
    > # otherwise test for which one just finished
    > #
    > unless( $line1 )
    > {
    > $flag = 1;
    > last;
    > }
    > unless( $line2 )
    > {
    > $flag = 2;
    > last
    > }
    a much better line check then the "junk" I wrote.
    > #
    > # now do the voodo on the two lines
    > #
    > chomp($line2);
    > chomp($line1);
    > print "$line2 : $line1\n";
    > }
    > # if one needs to know which ended first one
    > # can deal with that here...
    > if ( $flag )
    > {
    > print "more lines to read\n";
    > print "\tfrom TestFile \n" if ( $flag == 1 );
    > print "\tfrom logfile \n" if ( $flag == 2 );
    > } else {
    > print "both ended together\n";
    > }
    > # now one would be safe to close the files
    >
    > HTH.
    >
    Helps a bunch,

    Thanks Again to all who helped..

    Denis

    > ciao
    > drieux
    >
    > ---
    >
    >
    >
    denis@teachlinux.com Guest

  8. #7

    Default Re: Opening two files at once


    On Dec 23, 2003, at 9:03 AM, [email]denis@teachlinux.com[/email] wrote:
    [..]
    > Sorry for the curt thank you. You code looks really good.
    I'd never thought of it that way. What I think might
    be the elegant way of saying 'thank you' to folks who
    offer an idea/code/suggestion/BriefPsychoticEpisode would
    be something on the order of

    a. demonstrating what one had 'learned' from the code, idea, ....
    b. being able to help others with what one learned to others.

    Often times folks don't have the time, due to the press
    of what ever it was that got them tossing a 'current issue'
    into play...
    > On Mon, 22 Dec 2003, drieux wrote:
    >> On Dec 22, 2003, at 4:32 PM, [email]denis@teachlinux.com[/email] wrote:
    [..]
    >>
    >> So you might try the idea of
    >>
    >> my ($line1, $line2 , $flag );
    >> while ( 1 ) {
    >> $line1 = <TESTFILE>; # read them each
    >> $line2 = <LOGFILE>;
    >
    > never thought of reading two files one line at a time like this,
    > looks so easy once you showed me.
    the gooder news is that it 'solves' the problem
    you asked about - the information is that it is
    still not clear which way you really want to go.

    you might check out MJD's 'diff' in perl:
    <http://www.perl.com/language/ppt/src/diff/diff.mjd>
    >
    >> last unless ( $line1 || $line2 ); # if both empty exit loop
    >> #
    >> # otherwise test for which one just finished
    >> #
    >> unless( $line1 )
    >> {
    >> $flag = 1;
    >> last;
    >> }
    >> unless( $line2 )
    >> {
    >> $flag = 2;
    >> last
    >> }
    > a much better line check then the "junk" I wrote.
    actually upon reflection I thought

    $flag = 1 unless( $line1 );
    $flag = 2 unless( $line2 );
    last if $flag;

    would solve that part - but had it fanned out
    in case you wanted to do something specific with
    either case....

    The trade offs are basically simple:

    a. slurp in both files using a common slurper function
    then call a compare_slurpie()

    b. find a neater data model that will allow you to
    parse the files for the information you need, write
    one grot_data_from_file() method, call it twice and
    then a compare_grotted_data()

    c. pass in two file_names and grovel in parrallel as is
    line by line...
    [..]

    ciao
    drieux

    ---

    Drieux Guest

  9. #8

    Default Re: Opening two files at once

    On Tue, 23 Dec 2003, drieux wrote:
    >
    > On Dec 23, 2003, at 9:03 AM, [email]denis@teachlinux.com[/email] wrote:
    > [..]
    > > Sorry for the curt thank you. You code looks really good.
    >
    > I'd never thought of it that way. What I think might
    > be the elegant way of saying 'thank you' to folks who
    > offer an idea/code/suggestion/BriefPsychoticEpisode would
    > be something on the order of
    >
    > a. demonstrating what one had 'learned' from the code, idea, ....
    > b. being able to help others with what one learned to others.
    >
    > Often times folks don't have the time, due to the press
    > of what ever it was that got them tossing a 'current issue'
    > into play...
    >
    > > On Mon, 22 Dec 2003, drieux wrote:
    > >> On Dec 22, 2003, at 4:32 PM, [email]denis@teachlinux.com[/email] wrote:
    > [..]
    > >>
    > >> So you might try the idea of
    > >>
    > >> my ($line1, $line2 , $flag );
    > >> while ( 1 ) {
    > >> $line1 = <TESTFILE>; # read them each
    > >> $line2 = <LOGFILE>;
    > >
    > > never thought of reading two files one line at a time like this,
    > > looks so easy once you showed me.
    >
    > the gooder news is that it 'solves' the problem
    > you asked about - the information is that it is
    > still not clear which way you really want to go.
    >
    > you might check out MJD's 'diff' in perl:
    > <http://www.perl.com/language/ppt/src/diff/diff.mjd>
    >
    > >
    > >> last unless ( $line1 || $line2 ); # if both empty exit loop
    > >> #
    > >> # otherwise test for which one just finished
    > >> #
    > >> unless( $line1 )
    > >> {
    > >> $flag = 1;
    > >> last;
    > >> }
    > >> unless( $line2 )
    > >> {
    > >> $flag = 2;
    > >> last
    > >> }
    > > a much better line check then the "junk" I wrote.
    >
    > actually upon reflection I thought
    >
    > $flag = 1 unless( $line1 );
    > $flag = 2 unless( $line2 );
    > last if $flag;
    >
    > would solve that part - but had it fanned out
    > in case you wanted to do something specific with
    > either case....
    >
    > The trade offs are basically simple:
    >
    > a. slurp in both files using a common slurper function
    > then call a compare_slurpie()
    >
    > b. find a neater data model that will allow you to
    > parse the files for the information you need, write
    > one grot_data_from_file() method, call it twice and
    > then a compare_grotted_data()
    >
    > c. pass in two file_names and grovel in parrallel as is
    > line by line...
    > [..]
    >
    > ciao
    > drieux
    >
    > ---
    >
    >
    >
    Not really sure as to the final use of the code. I have a perl script I
    wrote back in Sept that opens a file, reads the file and runs a list of
    tests to do regression testing on LA-MPI (Los Alamos MPI). Currently I
    have a "kill" timer that runs during each test and kills the running test
    if it has a timeout. What I'm looking at is changing the timer depending
    on the test. Sounds simple enough, but I dont want to have to go in and modify
    the regression test list, it's almost 500 tests, and can change with
    each run.

    One of the issues is that I sometimes have to contend with other users on
    the system, so tests time out, even if they are running fine.. Was
    thinking along the lines of opening the tests file and also a stored time_to_run_tests file, double the value in
    the time_to_run_tests, and us it as the timeout value. However there may
    be times when there are not any time_to_run_tests files to use..

    So I'm just figuring it out as I go, and asking dumb questions..

    Thanks

    Denis



    denis@teachlinux.com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139