Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using HTML Template to fill a 2-dimensional table

by 7stud (Deacon)
on Feb 19, 2013 at 17:37 UTC ( #1019628=note: print w/ replies, xml ) Need Help??


in reply to Using HTML Template to fill a 2-dimensional table

This:

my @LINES; while (<DATA>) { chomp; push @LINES, $_; }

...is equivalent to this:

chomp (my @lines = <DATA>);

Who knows how that works, but it does. It's a perl idiom you need to know.

As for this:
my $split; for (@LINES) { my @row = split ('&', $_);

The only variable name you could think of, $split, is the same name as the perl function split() ? Don't ever do that. If you can't think up 10 unique variable names, you cannot be a computer programmer. Also, your variable names need to be descriptive. "Hey, Joe! I've got an array ref named '$split'. Guess what's in the array? Joe: Gold coins?"

Avoid writing $_ in your code. I suggest you read "Learning Perl 6th ed.". You need to learn modern perl.


Comment on Re: Using HTML Template to fill a 2-dimensional table
Select or Download Code
Re^2: Using HTML Template to fill a 2-dimensional table
by Anonymous Monk on Feb 20, 2013 at 03:13 UTC

    The only variable name you could think of, $split, is the same name as the perl function split() ? Don't ever do that. If you can't think up 10 unique variable names, you cannot be a computer programmer... Avoid writing $_ in your code. I suggest you read "Learning Perl 6th ed.". You need to learn modern perl.

    ETOOMUCHCAFFEINE

Re^2: Using HTML Template to fill a 2-dimensional table
by Athanasius (Monsignor) on Feb 20, 2013 at 09:47 UTC
    chomp (my @lines = <DATA>);

    Who knows how that works, but it does. It's a perl idiom you need to know.

    Yes, it’s a useful idiom — but surely not so mysterious?

    1. my @lines =
      Declares a lexical array variable and initialises it (with a list of array elements).

    2. @lines = <DATA>
      The LHS (left-hand side) of the assignment is an array, which gives list context to the expression on the RHS. When the readline or “diamond” operator <...> is placed in list context, it “reads until end-of-file is reached and returns a list of lines.” Lines are defined as the text between successive input record separators. The input record separator, stored in $/, “is newline by default.” See readline and General Variables in perlvar.

    3. chomp(...);
      When chomp is given a list, it chomps (removes the input record separator from) each element in the list.

    That wasn’t really so hard, was it? ;-)

    Avoid writing $_ in your code.

    Yes, avoid writing $_ explicitly where possible. But be aware that an explicit $_ is often needed; for example:

    • my @doubles = map { $_ * 2 } @numbers;

    • $couples{$_} eq 'Betty' && print for keys %couples;  # Find Betty's husband

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-11-23 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls