Professional Web Applications Themes

Problem iterating over diamond (while<FILE>) - PERL Beginners

Hi, Suppose I have a data file that contain these lines: output1 output2 when I run the code below with: perl mycode.pl -f datafile it gives: Trial 1 output1 output2 Trial 2 instead of: Trial 1 output1 output2 Trial 2 output1 output2 Can we actually loop over the 'while diamond'? Please kindly advice how can I overcome this problem. Thanks so much for your time. Regards, Edward WIJAYA SINGAPORE __BEGIN__ use strict; use warnings; use Getopt::Std; our $opt_f; getopts('f:'); open INFILE, "<$opt_f" or die "$0: Can't open file $opt_f: $!"; my $trial = 2; #What's wrong with running a for ...

  1. #1

    Default Problem iterating over diamond (while<FILE>)

    Hi,

    Suppose I have a data file that contain these lines:
    output1
    output2

    when I run the code below
    with: perl mycode.pl -f datafile
    it gives:

    Trial 1
    output1
    output2
    Trial 2


    instead of:

    Trial 1
    output1
    output2
    Trial 2
    output1
    output2

    Can we actually loop over the 'while diamond'?
    Please kindly advice how can I overcome this problem.

    Thanks so much for your time.

    Regards,
    Edward WIJAYA
    SINGAPORE


    __BEGIN__
    use strict;
    use warnings;
    use Getopt::Std;

    our $opt_f;
    getopts('f:');

    open INFILE, "<$opt_f" or die "$0: Can't open file $opt_f: $!";

    my $trial = 2;

    #What's wrong with running a for loop over the 'while' here?
    for ( my $t = 1 ; $t <= $trial ; $t++ ) {
    print "Trial ", $t, "\n";

    while (<INFILE>) {
    print;
    }
    }

    __END__
    Edward Guest

  2. #2

    Default Re: Problem iterating over diamond (while<FILE>)

    > Can we actually loop over the 'while diamond'? 

    I don't know if I'm skilled enough to answer this yet, but shouldn't you
    be using:

    my myarray = <FILE>;

    foreach (myarray) {
    do stuff
    }

     

    Gavin Guest

  3. #3

    Default Re: Problem iterating over diamond (while<FILE>)

    Gavin Henry said: 
    >
    > I don't know if I'm skilled enough to answer this yet, but shouldn't you
    > be using:[/ref]

    Nope, sorry. It helps if I *actually* read the rest of your e-mail.

    Sorry.
     
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]

    Gavin Guest

  4. #4

    Default RE: Problem iterating over diamond (while<FILE>)

    Edward Wijaya wrote: 

    When you reach the end of file on INFILE (during Trial 1), further reads
    will just return undef. You need to either close and reopen the file, or
    rewind the file using seek() before you can re-read the data.
    Bob Guest

  5. #5

    Default Re: Problem iterating over diamond (while<FILE>)

    Thanks a lot for your reply Bob.
    but can you be more specific:
     


    I tried seek() following "perldoc" like this:

    __BEGIN__
    for ( my $t = 1 ; $t <= $trial ; $t++ )
    {
    for ( $curpos = tell(INFILE); $_ = <INFILE>;
    $curpos = tell(INFILE))
    { print "Trial ", $t, "\n";
    while (<INFILE>) {
    print;}
    }
    seek(INFILE, $curpos, 0);
    }

    but doesn't seem to work.

    Also tried to close and 'reopen'.
    Doesn't seem to work as well.

    __BEGIN__
    for ( my $t = 1 ; $t <= $trial ; $t++ )
    {
    print "Trial ", $t, "\n";
    while (<INFILE>) {
    print;}
    }
    close INFILE;
    open INFILE; #is this how you reopen?
    __END__


    Hope you don't mind to elaborating it.

    Thanks again.

    Regards
    Edward WIJAYA
    Edward Guest

  6. #6

    Default RE: Problem iterating over diamond (while<FILE>)

    Edward Wijaya wrote: [/ref]

    What I mean is:

    for ( ...blah... ) {
    seek(INFILE, 0, 0); # <--- rewind file back to start
    while (<INFILE>) {
    ...
    }
    }

    Bob Guest

  7. #7

    Default Re: Problem iterating over diamond (while<FILE>)

    Many many thanks Bob!
    Glad to know I can do it in one-line.
     

    Edward Guest

  8. #8

    Default Re: Problem iterating over diamond (while<FILE>)

    Hi Edward,


    On Tue, 28 Sep 2004 11:20:39 -0400, Bob Showalter
    <com> wrote: [/ref][/ref]
    << SNIP >>

    Try this:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Getopt::Std;

    our $opt_f;
    getopts('f:');

    my $trial = 2;

    for ( my $t = 1 ; $t <= $trial ; $t++ ) {
    print "Trial $t\n";
    open INFILE, "<$opt_f" or die "$0: Can't open file $opt_f: $!";

    while (<INFILE>) {
    print;
    }
    }

    As a note, in the above code, you used a line:
    print "Trial ", $t, "\n";
    But I changed it to:
    print "Trial $t\n";

    The fun part of using double quotes (" ") is that Perl will
    interpolate (think "translate") any variables inside those double
    quotes that it finds! Helps with reducing your typing AND has the
    added benifit of making your code more readable!

    When you use 'open' twice on the same file handle, it will first close
    that file handle, then RE-open it with the input ready to go at the
    beginning again!
    Errin Guest

  9. #9

    Default Re: Problem iterating over diamond (while<FILE>)

    Nitpick - the "diamond operator" is <>. <INFILE>
    would be use of the "readline operator".

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/
    Peter Guest

  10. #10

    Default Re: Problem iterating over diamond (while<FILE>)

    On 29 Sep 2004 14:06:33 -0000, Peter Scott <com> wrote:
     

    Thanks for making it precise Peter.
    Glad that I learnt that.
    Forgive me for my limited Perl vocabulary.

    Regards,
    Edward WIJAYA
    SINGAPORE
    Edward Guest

Similar Threads

  1. Iterating through an XML Dataset
    By Richard Morey in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: June 1st, 03:41 AM
  2. Replies: 15
    Last Post: February 29th, 11:21 AM
  3. Iterating through controls (VB)
    By Jeremy in forum ASP.NET General
    Replies: 1
    Last Post: July 31st, 04:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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