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

  • Home
  • SEARCH
  • 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 6126375
In Process

The Archive Base Latest Questions

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

The following Perl script cureently reads in an html file and strips off what

  • 0

The following Perl script cureently reads in an html file and strips off what I don’t need. It also opens up a csv document which is blank.

My problem being is I want to import the stripped down results into the CSV’s 3 fields using Name as field 1, Lives in as field 2 and commented as field 3.

The results are getting displayed in the cmd prompt but not in the CSV.

use warnings; 
use strict;  
use DBI;
use HTML::TreeBuilder;  
use Text::CSV;

open (FILE, 'file.htm'); 
open (F1, ">file.csv") || die "couldn't open the file!";


my $csv = Text::CSV->new ({ binary => 1, empty_is_undef => 1 }) 
    or die "Cannot use CSV: ".Text::CSV->error_diag (); 

open my $fh, "<", 'file.csv' or die "ERROR: $!"; 
$csv->column_names('field1', 'field2', 'field3'); 
while ( my $l = $csv->getline_hr($fh)) { 
    next if ($l->{'field1'} =~ /xxx/); 
    printf "Field1: %s Field2: %s Field3: %s\n", 
           $l->{'field1'}, $l->{'field2'}, $1->{'field3'} 
} 
close $fh; 

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";
} 

An example of the html is:

<div class="postbody">
    <h3><a href "foo">Re: John Smith <span class="posthilit">England</span></a></h3>
    <div class="content">Is C# better than Visula Basic?</div>
</div>
  • 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:21:55+00:00Added an answer on May 23, 2026 at 4:21 pm

    You don’t actually write anything to the CSV file. Firstly, it isn’t clear why you open the file for writing and then later for reading. You then read from the (now empty) file. Then you read from the HTML, and display the contents you want.

    Surely you will need to write to the CSV file somewhere if you want data to appear in it!

    Also, it’s best to avoid barewords for file handles if you want to use them through Text::CSV.

    Maybe you need something like:

    my $csv = Text::CSV->new();
    $csv->column_names('field1', 'field2', 'field3');
    open $fh, ">", "file.csv" or die "new.csv: $!";
    ...
    # As you handle the HTML
    $csv->print ($fh, [$name, $location, $comment]);
    ...
    close $fh or die "$!";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following perl script (Showing down) print the following XML file but the XML
I wrote the following Perl script (below) in order to create simple XML file.
I have written the following Perl script- use HTML::TreeBuilder; my $html = HTML::TreeBuilder->new_from_content(<<END_HTML); <span
I have the following perl script. I need it to invalidate all cookies that
The following Perl script ( my.pl ) can read from either the file in
I have the following Perl script with is meant to indent a XML file
Within a Perl script I need to process the following string: 426852 total The
I have the following perl script: $myVariable = some value; //... some 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

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.