Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Html - dynamic array

by imrags (Monk)
on Jul 24, 2009 at 07:35 UTC ( #782859=perlquestion: print w/ replies, xml ) Need Help??
imrags has asked for the wisdom of the Perl Monks concerning the following question:

Here's something that was lingering in my mind for sometime. I use dynamically named
arrays a lot. I find using them much better when i'm not sure about the quantity of data i have
For example:
$te = HTML::TableExtract->new(keep_headers=> ["Customers","Samples","V +iolations","Availability"]); $te->parse($final_content); $arr_name = "ARR"; my $pages=0; foreach $ts ($te->tables) { print "Table Number(" , join(',',$ts->coords), "):\n"; $rws=0; foreach $row ($ts->rows) { $temp = join (",",@$row); my @row_ele = split(/,/,$temp); print "$pages,$rws\n"; ${$arr_name{$pages}}[$rws]=\@row_ele; # print ${$arr_name{$pages}}[$rws]->[1]."\n"; $rws++; } $pages++; }
Here the $final_content is html data with some tables.
Though this code works absolutely fine and with no errors, i still wonder if I should
use something else other than dynamically named arrays.
I use them in almost anything that deals with threads/html or with data for
which i'm not sure about the quantity...Just curious!
Raghu

Comment on Html - dynamic array
Download Code
Re: Html - dynamic array
by Anonymous Monk on Jul 24, 2009 at 07:52 UTC
Re: Html - dynamic array
by davorg (Chancellor) on Jul 24, 2009 at 07:56 UTC

    See "Why it's stupid to 'use a variable as a variable name'" - Part 1, Part 2, Part 3.

    There are good reasons why use strict makes symbolic references (aka "using variables as variable names") illegal. It's a really bad idea.

    In your case, I can't see any advantage that you have over just using an array called @ARR (though a better naming strategy might be nice). In the general case, you're almost always better off using hashes to store references to arrays.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: Html - dynamic array
by GrandFather (Cardinal) on Jul 24, 2009 at 08:05 UTC

    I don't know what you think that does, but it doesn't do it! I strongly recommend that you always use strictures (use strict; use warnings;). Under strictures the line containing ${$arr_name{$pages}}[$rws]=\@row_ele; generates the error:

    Global symbol "%arr_name" requires explicit package name at ...

    What you are achieving is autovivifying an entry in the package hash %arr_name. Over time your script will accumulate a hash entry for each page. If something of that sort is your intent then it would be better written using an explicit array of array (AOA):

    use strict; use warnings; use HTML::TableExtract; my $te = HTML::TableExtract->new ( keep_headers => ["Customers", "Samples", "Violations", "Availabili +ty"] ); $te->parse ($final_content); my @pages; foreach my $ts ($te->tables ()) { print "Table Number(", join (',', $ts->coords), "):\n"; $rws = 0; push @pages, []; foreach $row ($ts->rows) { my @row_ele = split /,/, join ",", @$row; push @{$pages[-1]}, \@row_ele; print scalar @pages, ',', scalar @{$pages[-1]}, "\n"; } }

    True laziness is hard work
Re: Html - dynamic array
by ikegami (Pope) on Jul 24, 2009 at 08:10 UTC
    Another note,
    my $rws = 0; for (...) { $foo[$rws] = ...; $rws++; }
    is a weird/convoluted way of doing
    for (...) { push @foo, ...; }

    You have two instances of unnecessarily using a counter.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://782859]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-08-21 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls