Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:50:58+00:00 2026-05-23T16:50:58+00:00

I have the following Perl, open (FILE, ‘file.htm’); my $tree = HTML::TreeBuilder->new_from_content(do { local

  • 0

I have the following Perl,

open (FILE, 'file.htm');
my $tree = HTML::TreeBuilder->new_from_content(do { local $/; <FILE> }); 

for ( $tree->look_down( 'class' => 'postbody' ) )
{     
    my $location = $_->look_down( 'class' => 'posthilit' )->as_trimmed_text;     
    my $comment  = $_->look_down( 'class' => 'content' )->as_trimmed_text;     
    my $name     = $_->look_down( '_tag'  => 'h3' )->as_text;     

    $name =~ s/^Re:\s*//;     $name =~ s/\s*$location\s*$//;      
    print "Name: $name\nLives in: $location\nCommented: $comment\n";

    my $csv = Text::CSV->new(); 
    $csv->column_names('field1', 'field2', 'field3'); 
    $csv->print ($fh, [$name,$location,$comment]);
    open $fh, ">", "file.csv" or die "ERROR: $!";
    close $fh or die "$!";
}

This only outputs one line from the input file into the new ‘file.csv’ file.
How can I get it to put all the results into the results file.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:50:58+00:00Added an answer on May 23, 2026 at 4:50 pm

    The code as written only prints one line because you only call $csv->print once. Since you obviously have more than one line of data, you need to create a loop that prints all of the lines. Something like this:

    while (my ($name, $location, $comment) = next_record()) {
        $csv->print($fh, [$name, $location, $comment]);
    }
    

    I can’t give you any more detailed answer than this without seeing the rest of your code and knowing how the variables $name, $location, $comment are assigned.


    Update after the question was edited:

    You have a loop with for ($tree->look_down(...)), which is fine and takes the place of the while loop that I described above. However, as currently written, you’re making a new $csv object and a new file every time through the loop! That’s why you only every get one line in the output file. You need to take the following lines and put them before the beginning of the for loop:

    my $csv = Text::CSV->new(); 
    $csv->column_names('field1', 'field2', 'field3');
    open $fh, ">", "file.csv" or die "ERROR: $!";
    

    Inside the loop you only need to do this:

    $csv->print ($fh, [$name,$location,$comment]);
    

    Then once the loop has completed, you can do this:

    close $fh or die "$!";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following Perl script- use HTML::TreeBuilder; my $html = HTML::TreeBuilder->new_from_content(<<END_HTML); <span
I have a problem with the following code (Perl/Cygwin) #!/usr/bin/perl open FILE, >, filename.txt
Right now, I have the following Perl code my $tmpl1="download1_video.html" if $file->{file_name}=~/\.(avi|divx|mkv|flv|mp4|wmv)$/i; $tmpl1||="download1.html"; so
I have basically the following perl I'm working with: open I,$coupon_file or die Error:
I have the follwoing script #!/usr/bin/perl open IN, /tmp/file; s/(.*)=/$k{$1}++;$1$k{$1}=/e and print while <IN>;
I have downloaded the following file: rawdata_2001.text and I have the following perl code:
I have to maintain the following Perl script: #!/usr/bin/perl -w die Usage: $0 <file1>
I have an interesting problem. I wrote the following perl script to recursively loop
I have the following regular expression that works fine in perl: Classification:\s([^\n]+?)(?:\sRange:\s([^\n]+?))*(?:\sStructural Integrity:\s([^\n]+))*\n The
I have Perl code similar to the following: # -- start -- my $res;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.