Beginner Filehandle question

Discussion in 'Perl Misc' started by Chris L., Jan 17, 2006.

  1. Chris L.

    Chris L. Guest

    Dear Experienced Perl programmmer:
    I am having trouble understanding exactly why I can print the below
    program just fine through my IDE console, but when I try printing the
    exact same array (@goose) to a text file through a file handle, it only
    prints the last entry...
    Again, I have pasted my code below, and would appreciate any advice as
    to how to remedy this problem. I have gone over all three of my Perl
    books and searched Google for the last 4 hours, and cannot find the
    answer...
    Thank you very much for your help
    Chris

    -------------------------------------------------------------------------------------------------------------------------------------
    #!/usr/bin/perl -w
    use strict;

    open(ORIGINN, "C:\\Documents and Settings\\Admin\\My
    Documents\\ci_p.txt")|| die "Cannot open bci_pep.txt: $!";
    #opening my text file

    my (@DATA)=<ORIGINN>;
    close (ORIGINN);
    #copied my text file to array @DATA

    my@UUOO = grep /Entry/, @DATA;
    #grabbed each line in my array starting with Entry

    foreach my$cone (@UUOO){
    my $var= substr($cone, 28, -1);
    #selected a particular word from each line in UUOO array

    my @goose="$var\n" ;

    print @goose;
    #prints out in the Active State Komodo IDE output screen PERFECTLY!

    open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
    print LOGF @goose;
    close(LOGF);
    #prints only the last entry to the text file "entry.txt :(
    Howcome??
    }
    Chris L., Jan 17, 2006
    #1
    1. Advertising

  2. Chris L.

    J. Gleixner Guest


    > foreach my$cone (@UUOO){


    > open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
    > print LOGF @goose;
    > close(LOGF);
    > #prints only the last entry to the text file "entry.txt :(
    > Howcome??
    > }
    >


    Because your open() is within your foreach loop. Each time through the
    loop it creates/over-write your 'entry.txt' file and write whatever was
    in @goose.

    If you want to append information, use '>>';

    perldoc -f open
    J. Gleixner, Jan 17, 2006
    #2
    1. Advertising

  3. Chris L.

    Eric Martin Guest

    Chris L. wrote:
    > -------------------------------------------------------------------------------------------------------------------------------------
    > #!/usr/bin/perl -w
    > use strict;
    >
    > open(ORIGINN, "C:\\Documents and Settings\\Admin\\My
    > Documents\\ci_p.txt")|| die "Cannot open bci_pep.txt: $!";
    > #opening my text file
    >
    > my (@DATA)=<ORIGINN>;
    > close (ORIGINN);
    > #copied my text file to array @DATA
    >
    > my@UUOO = grep /Entry/, @DATA;
    > #grabbed each line in my array starting with Entry
    >
    > foreach my$cone (@UUOO){
    > my $var= substr($cone, 28, -1);
    > #selected a particular word from each line in UUOO array
    >
    > my @goose="$var\n" ;
    >
    > print @goose;
    > #prints out in the Active State Komodo IDE output screen PERFECTLY!
    >
    > open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
    > print LOGF @goose;
    > close(LOGF);
    > #prints only the last entry to the text file "entry.txt :(
    > Howcome??
    > }


    Try this:
    use strict;

    my @goose;
    open(ORIGINN, "C:/Documents and Settings/Admin/My Documents/ci_p.txt")
    || die "Cannot open bci_pep.txt: $!";
    while (<ORIGINN>) {
    if (/^Entry/) {
    push(@goose, substr($_, 28, -1)."\n");
    }
    }
    close (ORIGINN);
    open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
    print LOGF @goose;
    close(LOGF);

    Thanks,
    Eric
    Eric Martin, Jan 17, 2006
    #3
  4. Eric Martin wrote:
    > Chris L. wrote:
    >> -------------------------------------------------------------------------------------------------------------------------------------
    >> #!/usr/bin/perl -w
    >> use strict;
    >>
    >> open(ORIGINN, "C:\\Documents and Settings\\Admin\\My
    >> Documents\\ci_p.txt")|| die "Cannot open bci_pep.txt: $!";
    >> #opening my text file
    >>
    >> my (@DATA)=<ORIGINN>;
    >> close (ORIGINN);
    >> #copied my text file to array @DATA
    >>
    >> my@UUOO = grep /Entry/, @DATA;
    >> #grabbed each line in my array starting with Entry
    >>
    >> foreach my$cone (@UUOO){
    >> my $var= substr($cone, 28, -1);
    >> #selected a particular word from each line in UUOO array
    >>
    >> my @goose="$var\n" ;
    >>
    >> print @goose;
    >> #prints out in the Active State Komodo IDE output screen PERFECTLY!
    >>
    >> open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
    >> print LOGF @goose;
    >> close(LOGF);
    >> #prints only the last entry to the text file "entry.txt :(
    >> Howcome??
    >> }

    >
    > Try this:
    > use strict;
    >
    > my @goose;
    > open(ORIGINN, "C:/Documents and Settings/Admin/My Documents/ci_p.txt")
    > || die "Cannot open bci_pep.txt: $!";


    so if it can't open ci_p.txt, you'll get the message
    "Cannot open bci_pep.txt...."

    :)

    Is best to use variables in this kind of situation to avoid misleading
    oneself. Also, lexically scoped filehandles are now generally advised,
    rather than global filehandles.

    my $filename = "C:/Documents and Settings/Admin/My Documents/ci_p.txt";
    open (my $originn, "<",$filename) || die "Could not open $filename for
    read: $!";

    while(<$originn>){
    .....

    regards,

    Mark
    Mark Clements, Jan 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eduard W. Lohmann
    Replies:
    1
    Views:
    627
  2. Bill

    key as filehandle error

    Bill, Sep 1, 2004, in forum: Perl
    Replies:
    2
    Views:
    524
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    555
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    944
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    194
    7stud --
    Apr 12, 2011
Loading...

Share This Page