in reply to adding rank to a match in while loop.

I really hate coding anything web related. I force myself to though when I get a scratch, so that I can do it when I really need to. Have a look at:
#!/usr/bin/perl use strict; use warnings; use HTML::TableContentParser; use HTML::TokeParser::Simple; use WWW::Mechanize; use Data::Dumper; my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->get( '' ); my @gene_score; my $table = HTML::TableContentParser->new()->parse( $mech->content() ) +; for my $rank ( 1 .. $#{ $table->[0]{rows} } ) { my $cells = $table->[0]{rows}[$rank]{cells}; push @gene_score, { Rank => $rank, Ensembl_ID => Get_Label( $cells->[0]{data} ), RefSeq_ID => $cells->[1]{data}, Gene_Symbol => $cells->[2]{data}, Band => $cells->[3]{data}, Gene_Name => $cells->[4]{data}, Probability => $cells->[5]{data}, Known_Phenotype => $cells->[6]{data}, }; } sub Get_Label { my $link = shift; my $p = HTML::TokeParser::Simple->new( \$link ); while ( my $token = $p->get_token ) { next if ! $token->is_start_tag; return $token->return_attr( 'href' ) =~ /gene=(\w+)$/; } }
This can obviously be made less verbose, but I wanted it to be clear where the data was coming from.

Cheers - L~R

Added the $table->[0]{rows}[$rank]{cells} shortcut

Replies are listed 'Best First'.
Re^2: adding rank to a match in while loop.
by BioGeek (Hermit) on Aug 13, 2004 at 15:41 UTC
    Thanks, Limbic Region.

    That ugly regex in my code was written back in those days when I just started learning Perl (read: some three months ago). At that point I still had to figure out what exactly a reference was. But in the meantime I also discovered the HTML module.