Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Insert a variable without quotes

by LaneM1234 (Initiate)
on Jun 21, 2012 at 20:29 UTC ( #977707=perlquestion: print w/ replies, xml ) Need Help??
LaneM1234 has asked for the wisdom of the Perl Monks concerning the following question:

I have a code that looks through an HTML file and grabs the code between a day, (in the code, 4/23/012), and the end of that table column. The code works when I input the day manually, but if I try to make it a variable (i.e. $date), the code breaks down. If I add quotes to the day in the code, it breaks as well. I am assuming the code breaks down because of the quotes, which must be added in when I try and use a variable. Please help! I'm so close!

use strict; use LWP::Simple; print "What day?"; my $date = <>; my $url = ' +blic_html/1112hcal/1112hcal.htm'|| die "Specify URL on the cmd line"; my $html = get ($url); $html =~ m{4/23/012(.*?)<td width="3%" style='width:3.12%;border:none; +padding:0in 5.4pt 0in 5.4pt'>}gism; print "$1\n"; use Fcntl; #The Module print "content-type: text/html \n\n"; #The header sysopen (HTML, 'myhtml.html', O_RDWR|O_EXCL|O_CREAT, 0755); printf HTML "$1\n"; close (HTML);

Comment on Insert a variable without quotes
Download Code
Re: Insert a variable without quotes
by frozenwithjoy (Curate) on Jun 21, 2012 at 20:36 UTC
    Can you please elaborate on what you mean by "the code breaks down"? Maybe show the output when it works and when it doesn't.
Re: Insert a variable without quotes
by Eliya (Vicar) on Jun 21, 2012 at 20:48 UTC
    my $date = <>;

    You don't show how you actually tried to use the value, but I suspect the problem is that you don't chomp $date, so it still contains the newline...

Re: Insert a variable without quotes
by ww (Bishop) on Jun 21, 2012 at 21:16 UTC

    chomp your input. The trailing newline is giving you the problems you're attributing to quotes.

    Line 7 probably takes a much bigger bite than you need. The td and style attributes have nothing to do with your assignment. But, OTOH, you might like to say:
    $html =~ m{$date(.*?)(?=</tr>)}gms;
    to make sure you get the whole mess.

    Did /me just call that assignment page a mess? Yep! It appears that the maker is using MS Word (or possibly, some other M$, gefuksperated generator of HTML) producing excessively verbose, unnecessarily obscure code. You have my sympathy, because the best you'll get directly from this site is something like this: What day? 4/24/012 Wide character in print at D:\_Perl_\pl_test\ line 14, <> lin +e 1. <o:p></o:p></span></p> </td> <td width="85%" valign=top style='width:85.66%;border:none;backgroun +d:white; padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal style='tab-stops:decimal .5in left 109.85pt'><b style='mso-bidi-font-weight:normal'><span style='font-size:10.0pt'>A +P Exam-<span class=SpellE>ish</span> Test, Part IA (multiple <span class=GramE>ch +oice</span>, no calculator).</span></b><span style='font-size:10.0pt'> We will st +art at 7:55 sharp: 28 questions in 55 minutes.<br> <br> HW due (will not be collected until next week): another 70 minutes&# +915;اض worth of AP review problems, double dose over the long weekend.<o:p></o:p></s +pan></p> </td> <td width="3%" style='width:3.12%;border:none;padding:0in 5.4pt 0in +5.4pt'> <p class=MsoNormal><span style='font-size:10.0pt'><o:p>&nbsp;</o:p>< +/span></p> </td>

    Dealing with the encoding that produces the initial "wide character" report would be trivial, but getting rid of the proprietary tags and css will be a major PITA.

    FTR, the following code produced the above output but it still has problems -- such as the omission of a line to IMMEDIATELY assign the value in $1 to a named $var and the uninformative and misleading message in your or die:

    #!/usr/bin/perl use 5.014; use LWP::Simple; print "What day? "; my $date = <>; chomp $date; my $url = ' +blic_html/1112hcal/1112hcal.htm'|| die "Specify URL in the form 'm/dd +/0yy'"; my $html = get ($url); $html =~ m{$date(.*?)(?=</tr>)}gms; print "$1 \n";

    And BTW, this smells even more like homework, now, given the assignment date is nearly 2 months past.

Re: Insert a variable without quotes
by davido (Archbishop) on Jun 21, 2012 at 21:59 UTC

    chomp $date; $html =~ m{\Q$date\E(.*?)..............}ism;

    Key components: chomp, quotemeta (or the \Q and \E metacharacters, and omitting the /g modifier, which isn't doing anything for you here (you're only matching once, and not in list context anyway).

    Also, you're working to hard at printing your output file.

    open my $HTML, '>', 'myhtml.html' or die $!; print $HTML "$1\n"; close $HTML or die $!;

    I don't think your content-type line is doing anything for you here unless you're creating a CGI script, in which case you probably wouldn't be opening an output file, but instead would just be printing to STDOUT (the default).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977707]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-05-23 00:39 GMT
Find Nodes?
    Voting Booth?

    In my home, the TV remote control is ...

    Results (465 votes), past polls