For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > March 2004 > Newbie question on iterating through arrays, hashes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie question on iterating through arrays, hashes
David R.

2004-03-28, 10:13 pm

I have an overly large html from which I'm trying to extract specific
information to create a number of different html pages, each being a
subset of the original. The large html page contains tables in the
following form:

<tr> <td>Granny Smith</td> <td>Yellow</td> </tr>
<tr> <td>Kumquats</td> <td>Orange</td> </tr>
<tr> <td>Asparagus</td> <td>Green</td> </tr>
<tr> <td>Macintosh</td> <td>Red</td> </tr>
<tr> <td>Acorns</td> <td>Squirrels love 'em</td> </tr>

Iterating through each element of a single array consisting of apple
names to create an apples.html page is straight forward enough. What I
can't figure out is how best to approach doing the same if I want to
generate a dozen different html pages for a dozen categories of fruits,
vegetables, and nuts.

Thanks in advance for any suggestions.

-------------------begin-----------------------------------------
#!/usr/bin/perl
# readfile.pl
# Parse an overly large html file containing tables of fruits and
# vegetable information to create a series of separate html files
# based fruit, vegetable and nut types.

use strict;
use warnings;

open( INFILE, "E:/PROJECTS/Fruits_Vegetables_Nuts/default.html" )
or die( "Cannot open INFILE for reading: $!" );

open( APPLES, ">E:/PROJECTS/Fruits_Vegetables_Nuts/apples.html" )
or die( "Cannot open apples.html for writing: $!" );

# array for apple category
my @apples = ( 'grannysmith', 'macintosh', 'spy' );

# Insert series of print statements to create html page

while ( <INFILE> ) {
foreach my $type(@apples) {
if ( /$type/i ) {
print( RESULTS );
}
}
}

# Insert series of print statements to finish creating html page

close( INFILE ) or die( "Cannot close file: $!" );
close( RESULTS ) or die( "Cannot close file: $!" );
-------------------end-------------------------------------------


Gunnar Hjalmarsson

2004-03-28, 10:13 pm

David R. wrote:
> Iterating through each element of a single array consisting of
> apple names to create an apples.html page is straight forward
> enough. What I can't figure out is how best to approach doing the
> same if I want to generate a dozen different html pages for a dozen
> categories of fruits, vegetables, and nuts.


Don't know about "best", but this is one approach:

open APPLES, '> E:/PROJECTS/Fruits_Vegetables_Nuts/apples.html'
or die "Cannot open apples.html for writing: $!";

open BANANAS, '>E:/PROJECTS/Fruits_Vegetables_Nuts/bananas.html'
or die "Cannot open bananas.html for writing: $!";

sub apple_print { print APPLES shift }
sub banana_print { print BANANAS shift }

# hash for fruit category
my %fruits = (
granny => \&apple_print,
macintosh => \&apple_print,
spy => \&apple_print,
somethingelse => \&banana_print,
);

while ( <INFILE> ) {
for my $type ( keys %fruits ) {
$fruits{$type}->($_) if /$type/i;
}
}

I chose to create a small subroutine for printing to each file, and a
hash whose values are references to the applicable subs.

HTH

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gunnar Hjalmarsson

2004-03-28, 10:13 pm

Gunnar Hjalmarsson wrote:
> I chose to create a small subroutine for printing to each file, and
> a hash whose values are references to the applicable subs.


Or, inspired by a thread in comp.lang.perl.misc, this is a similar
solution but without the subroutines:

open APPLES, '> E:/PROJECTS/Fruits_Vegetables_Nuts/apples.html'
or die "Cannot open apples.html for writing: $!";

open BANANAS, '> E:/PROJECTS/Fruits_Vegetables_Nuts/bananas.html'
or die "Cannot open bananas.html for writing: $!";

# hash for fruit category
my %fruits = (
granny => *APPLES,
macintosh => *APPLES,
spy => *APPLES,
somethingelse => *BANANAS,
);

while ( <INFILE> ) {
for my $type ( keys %fruits ) {
print { $fruits{$type} } $_ if /$type/i;
}
}

Aren't hashes wonderful! :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

David R.

2004-03-29, 5:32 am

In <c47els$2e173v$1@ID-184292.news.uni-berlin.de>, noreply@gunnar.cc
wrote
> Gunnar Hjalmarsson wrote:
>
> Or, inspired by a thread in comp.lang.perl.misc, this is a similar
> solution but without the subroutines:
>
> open APPLES, '> E:/PROJECTS/Fruits_Vegetables_Nuts/apples.html'
> or die "Cannot open apples.html for writing: $!";
>
> open BANANAS, '> E:/PROJECTS/Fruits_Vegetables_Nuts/bananas.html'
> or die "Cannot open bananas.html for writing: $!";
>
> # hash for fruit category
> my %fruits = (
> granny => *APPLES,
> macintosh => *APPLES,
> spy => *APPLES,
> somethingelse => *BANANAS,
> );
>
> while ( <INFILE> ) {
> for my $type ( keys %fruits ) {
> print { $fruits{$type} } $_ if /$type/i;
> }
> }
>
> Aren't hashes wonderful! :)
>
>


Wonderful? LOL! My palms sweat every time I see AMGYAWTD (Ach mein
Gott Yet Another Way To Do It). What I've got in front of me I would
ordinarily do with boilerplate javascript show/hide functions in
combination with vi gymnastics, but I thought I'd take the opportunity
to learn something more about Perl which, if I think about it, may be an
endless proposition. Rewarding, but endless.

Thanks for the help.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com