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

Sorry if I post in wrong place at first time at "cool use the perl". Anyway here, I am learning on Perl and I wrote the array so I am wondering if that's good or what? I just want to improvement my perl skill & knowledge. I am trying to make array to read from bottom to top in the database. The inside of database looks like this "Date|Title|URL|Comments", so here's array code. Any feedback on array?
This is example of database. 07.11.2001|Hello World1|www.domain1.com|Your Comments in Here 07.12.2001|Hello World2|www.domain2.com|Your Comments in Here 07.13.2001|Hello World3|www.domain3.com|Your Comments in Here 07.14.2001|Hello World4|www.domain4.com|Your Comments in Here
#This is array to read from bottom to top. my @all_array; open(FILE,"$datadir/$datafile") || &Errormsg("Please correct your path + or chmod of database."); while (<FILE>) { @_ = split /\|/; my $hash_ref = { DATE => $_[0], TITLE => $_[1], URL => $_[2], COMMENTS => $_[3], }; push @all_array, $hash_ref; } close(FILE); foreach (reverse @all_array) { print "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpa +dding=\"3\">"; print " <tr>"; print " <td><font color=\"orange\"><b>$_->{'TITLE'}</b></font>< +br><font size=\"1\">Posted - $_->{'DATE'}</font></td>"; print " </tr>"; print " <tr>"; print " <td>$_->{'COMMENTS'} (<a href=\"$_->{'URL'}\" target=\" +_blank\">more information</a>)</td>"; print " </tr>"; print "</table><br>"; }
Thanks, Jeremy

Replies are listed 'Best First'.
Re: What's the best method of Array function?
by abstracts (Hermit) on Jul 14, 2001 at 16:15 UTC

    Hello,

    You got good replies for using the <<HEREDOC. I suggest making this modification to the while loop.

    while (<FILE>) { my %hash; # make a new hash and assign values # keys are DATA .. COMMENTS, and values come from # the split. @hash{qw/DATE TITLE URL COMMENTS/} = split /\|/; push @all_array, \%hash; } close(FILE);
    Or, raise your wand and say:
    @cols = qw/DATE TITLE URL COMMENTS/; @all_array = map{ my %h; @h{@cols} = split /\|/; \%h}<FILE>;
    Aziz,,,
Re: What's the best method of Array function?
by synapse0 (Pilgrim) on Jul 14, 2001 at 14:33 UTC
    Doesn't look too bad. A few comments tho. The process will suffer mem issues if the db is large, since you're reading the whole thing into memory, tho i can't think of a particular way around that to get the reverse you want.
    Also, there's no real reason to play with @_ and $_.. You can just as easily assign any array variable here, and @_/$_ can sometimes be a bad things to play with due to their magical nature.
    also, your last foreach may be more readable if you didn't use double quotes to surround it.. there's a few things you can use here.. one of them is the qq() variant of quotes.. Personally i'd probably do:
    foreach $hash (reverse @all_array) { print <<HTML; <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td><font color="orange"><b>$hash->{'TITLE'}</b></font><br><font s +ize="1">Posted - $hash->{'DATE'}</font></td> </tr> <tr> <td>$hash->{'COMMENTS'} (<a href="$hash->{'URL'}" target="_blank"> +more information</a>)</td> </tr> </table><br> HTML }
    -Syn0
Re: What's the best method of Array function?
by mattr (Curate) on Jul 14, 2001 at 17:25 UTC
    You could also make it more readable by splitting code from the HTML with a template. Instead of just pipes I usually name my tags like %URL% so they can be moved around, or you could use HTML::Template for a bigger project. One advantage is that if you put the template in a separate file you can view it in a browser or have someone without coding experience edit the HTML design.
    #!/usr/local/bin/perl -w use strict; my ($i,%hash,@row); my $buf = ""; while (<DATA>) { $buf .= $_; } my @sections = split(/\|/,$buf); my @layout = qw(TITLE DATE COMMENTS URL); open(FILE,"0614db"); while (<FILE>) { @row = split(/\|/,$_); foreach $i (qw(DATE TITLE URL COMMENTS)) { $hash{$i} = pop @row; } foreach $i (0..$#layout) { print $sections[$i],$hash{$layout[$i]}; } print $sections[$#layout+1],"\n"; } close(FILE); __DATA__ <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td><font color="orange"><b>|</b></font><br> <font size="1">Posted - |</font> </td> </tr> <tr> <td>| (<a href="|" target="_blank"> more information</a>) </td> </tr> </table><br>