http://www.perlmonks.org?node_id=891744

jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting: "Use of uninitialized value $fdr..."

The problem arises when I try to assign a numeric value to an array that houses the 10 different variables I'm tryin to give the values to (It's towards the very bottom) Here's my code:

use strict;
use warnings;
use LWP::Simple;
use HTML::TableExtract;

my $money = get("http://finance.yahoo.com
/q/ks?s=MNDO+Key+Statistics");
my $p = new HTML::TableExtract();

$p->parse($money);

my $table = $p->table(2,9);

#foreach my $row ($table->rows ()) {
# ! defined and $_ = ' ' for @$row;
# print "$row->[0]\n";
# print "$row->1\n";
# print "@$row\n";
#}

#Forward Annual Dividend Rate = $fdr
#Forward Annual Dividend Yield = $fdy
#Trailing Annual Dividend Rate = $tdr
#Trailing Annual Dividend Yield = $tdy
#5 Year Average Dividend Yield = $fyay
#Payout Ratio = $pr
#Dividend Date = $dd
#Ex-Dividend Date = $edd
#Last Split Factor = $lsf
#Last Split Date = $lsd

my @Dividend_Values = (my $fdr, my $fdy, my $tdr, my $tdy, my$fyay, my $pr, my $dd, my $edd, my $lsf, my $lsd);

for (my $count=0; $count < 11; $count++)
{
my $cell = $table->cell($count,1);
#print "$cell \n";
$Dividend_Values$count = $cell;

}
print $fdr;

I love it when a program comes together - jdhannibal
  • Comment on Another Dumb Noob Question - Arrays & Scalar Variables

Replies are listed 'Best First'.
Re: Another Dumb Noob Question - Arrays & Scalar Variables
by roboticus (Chancellor) on Mar 07, 2011 at 05:06 UTC

    jdlev:

    Thanks for making the effort of making your code clear enough to read. Unfortunately, you worked too hard to do it. You could've just put <c> before your code and </c> after it, and it would all be formatted nicely. Just so you know...

    Anyway, as ikegami mentions, you didn't assign anything to $fdr. Since a variable is undefined until you assign something to it, then you need only assign the proper value to it before you use it. So perl is just letting you know that you're reading (and presumably using) a value that you haven't defined yet.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Another Dumb Noob Question - Arrays & Scalar Variables
by ikegami (Patriarch) on Mar 07, 2011 at 04:14 UTC

    You never assign anything to $fdr. I think you meant

    for my $count (0..10) { my $cell = $table->cell($count,1); $Dividend_Values[$count] = $cell; } my ($fdr, $fdy, $tdr, $tdy, $fyay, $pr, $dd, $edd, $lsf, $lsd) = @Dividend_Values;
Re: Another Dumb Noob Question - Arrays & Scalar Variables
by CountZero (Bishop) on Mar 07, 2011 at 07:27 UTC
    A bit more Perlish way of getting the data into your variables is:
    use Modern::Perl; use LWP::Simple; use HTML::TableExtract; my $money = get("http://finance.yahoo.com/q/ks?s=MNDO+Key+Statistics") +; my $p = new HTML::TableExtract(); $p->parse($money); my $table = $p->table(2,9); my ($fdr, $fdy, $tdr, $tdy, $fyay, $pr, $dd, $edd, $lsf, $lsd) = map{$ +table->cell($_,1)} (1 .. 10); say $fdr;

    Note that you also had an "off by one" error in your loop. The "Forward Annual Dividend Rate" is cell 1, 1 and not 0, 1.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James