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

I just know there's a better way to do this than what I am doing. Basically, I get the content of a webpage using HTTP::Tiny; the content looks like so:
<A TARGET="UI" HREF="/turnka/user_interaction?action=display_question +&UNIT_TO_BATCH=batch_UTFS-7-1369171801707%3A1%3A1&LOCATION=UTFS-7&QUE +STION_ID=CHECK_TIME">batch_UTFS-7-1369171801707:1:1</A></TD> <TD ALIGN="center"> UTFS-7</TD> <TD ALIGN="center"> CHECK_TIME</TD> <TD ALIGN="center"> Sys Check</TD> <TD ALIGN="center"> 70 h23 m</TD> <TD ALIGN="center"> Please Answer</TD> </TR>
Now, the page is dynamic. What I mean is that there could be no output like above, or there could be 10 outputs like above. I am trying to build a hash from the output, and doing it like so:
my $server = $self-{server}; + + my $response = $http->get("http://$server:8080/turnka/view"); + my $content = $response->{content}; + if ( $content !~ /No UI/ ) { my @ui = ( $content =~ /(<A\s+TARGET="UI" HREF="(.*?)"> + (?<!\=)(batch.*?)<\/A><\/TD> + <TD ALIGN.*?>(.*?)<\/TD> + <TD ALIGN.*?>(.*?)<\/TD> + <TD ALIGN.*?>(.*?)<\/TD> + <TD ALIGN.*?>(.*?)<\/TD> + <TD ALIGN.*?>(.*?)<\/TD>/gs + ); + foreach my $ui (@ui) { + $ui_hash{$server}{$3}{'batch'} = $2; + $ui_hash{$server}{$3}{'url'} = $1; + $ui_hash{$server}{$3}{'interaction'} = $4; + $ui_hash{$server}{$3}{'title'} = $5; + $ui_hash{$server}{$3}{'time'} = $6; + $ui_hash{$server}{$3}{'status'} = $7; + } }
I am doing this for multiple servers, so I am thinking that the hash should look something like: $hash{'server_name'}{'location'}{'url'} and so on... Is there an easy way to map the matches from a regex match to a hash? Looking at my code I know that the way I am going about this is really inefficient.

Replies are listed 'Best First'.
Re: Looking for a better way to build a hash from regex matches.
by hdb (Monsignor) on May 22, 2013 at 07:14 UTC

    The situation is probably complex enough to justify the use of a proper HTML parser, but if you want to stick to regex then using named matches could simplify your code a bit:

    $content =~ s/\n//g; if ( $content !~ /No UI/ ) { while ( $content =~ /<A\s+TARGET="UI"\s+HREF="(?<url>.*?)"> (?<batch>batch.*?)<\/A><\/TD> <TD\s+ALIGN.*?>(?<key>.*?)<\/TD> <TD\s+ALIGN.*?>(?<interaction>.*?)<\/TD> <TD\s+ALIGN.*?>(?<title>.*?)<\/TD> <TD\s+ALIGN.*?>(?<time>.*?)<\/TD> <TD\s+ALIGN.*?>(?<status>.*?)<\/TD>/gsxm ) { %{ $ui_hash{$server}{ $+{key} } } = %+; } }
Re: Looking for a better way to build a hash from regex matches.
by Anonymous Monk on May 22, 2013 at 03:13 UTC