joelnackman has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a program that extracts data from a contact list on the web. I am using the Spreadsheet::WriteExcel module to write the data to an Excel file. The section of code that writes to the file looks like this, where @people is an array of anonymous hashes:
foreach my $person (@people) { my @personData; push @personData, $person->{"name"}; push @personData, $person->{"email"}; push @personData, $person->{"phone"}; $worksheet->write_row($excelRow, 0, \@personData) or die "Writ +e failed: $!\n"; $excelRow++; }
The write_row seems to fail every time, but I haven't been able to figure out why. I've checked to make sure that the $worksheet variable actually exists when the write_row is executing, and as far as I can tell, it is. I think that possibly the problem might have something to do with the Tk in the program, because I was able to run the program successfully before I put in the interface code. Does the MainLoop() have some sort of effect on the rest (possibly variables) of the program?
Any help is very much appreciated. Thanks!#!/usr/bin/perl use warnings; use strict; use LWP::Simple; use Spreadsheet::WriteExcel; use Tk; my $excelRow = 0; my $URL; my @people; my $workbook = Spreadsheet::WriteExcel->new("names.xls"); my $worksheet = $workbook->add_worksheet("Names"); my $mw = MainWindow->new(); $mw->title("Data Extractor"); #Instructions $mw->Label(-text => "Enter a URL, and hit Extract.")->grid(-row => 0, -column => + 0, -columnspa +n => 3); #Interface $mw->Label(-text => "URL:")->grid(-row => 1, -column => 0, -sticky => 'w'); $mw->Entry(-textvariable => \$URL)->grid(-row => 1, -column => 1, -columnspan => + 3, -ipadx => '30' +); $mw->Button(-text => "Extract", -command => \&getData)->grid(-row => 2, -column => 0, -columnspan => 3, -pady => '10'); MainLoop(); sub getData() { my @data = split /\n/, get($URL); foreach my $line (@data) { if ($line =~ /<td/) { my @people = split /<\/td>/, $line; foreach my $person (@people) { if ($person =~ /mailto:/) { &extractData($person); } } } } foreach my $person (@people) { my @personData; push @personData, $person->{"name"}; push @personData, $person->{"email"}; push @personData, $person->{"phone"}; $worksheet->write_row($excelRow, 0, \@personData) or die "Writ +e failed: $!\n"; $excelRow++; } @people = undef; } sub extractData() { my $person = shift @_; my ($name, $email, $phone); if ($person =~ /<strong>(.*)<\/strong>/) { $name = $1; } if ($person =~ /\"mailto:(.*)\">\1/) { $email = convertHTMLEmail($1); } if ($person =~ /(\(\d{3}\) ?\d{3}-?\d{4})/) { $phone = $1; } push @people, {"name" => $name, "email" => $email, "phone" => $pho +ne}; } sub convertHTMLEmail() { my $emailString = shift @_; $emailString =~ s/&#//g; my @chars = split /;/, $emailString; my @convertedChars; foreach (@chars) { push @convertedChars, chr($_); } return join "", @convertedChars; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Excel File Problem
by liverpole (Monsignor) on May 20, 2006 at 15:50 UTC | |
by joelnackman (Beadle) on May 20, 2006 at 16:03 UTC | |
by liverpole (Monsignor) on May 20, 2006 at 16:12 UTC | |
|
Re: Excel File Problem
by dragonchild (Archbishop) on May 20, 2006 at 15:38 UTC | |
by joelnackman (Beadle) on May 20, 2006 at 15:53 UTC |