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

Hello Monks, I have used the below code to extract the columns from web page and i able to print them but i need to format the output as below please guide me in achieving this.
use LWP::UserAgent; use HTTP::Request; use HTML::TableExtract; my $ua = LWP::UserAgent->new(timeout => 10); my $url = 'http://foo/bar/index.php/acc_details'; my $request = HTTP::Request->new('GET',$url); my $response = $ua->request($request); if ($response->is_success){ my $te = HTML::TableExtract->new ( headers => [ 'host','login']); $te->parse($response->content); foreach my $ts ($te->table_states) { foreach my $row ($ts->rows) { if ($row !~ "disabled"){ print @$row; } } } } the output i am getting now is as below. xyz anand,pradeep abc shiva,magawal cde jpat,mayank but what i need is as below xyz anand xyz pradeep abc shiva abc magawal cde jpat cde mayank

Replies are listed 'Best First'.
Re: Help in formating the output
by moritz (Cardinal) on Jan 13, 2009 at 09:57 UTC
    So you need to split $row->[1] on a comma, and for each of the result print that and $row->[0].

    Given that you've managed to get as far as you did, that doesn't sound too complicated - just try it, and if you can't make it, come back and show us what you've tried.

      Thanks for pointing out that. I have tried this...
      foreach my $ts ($te->table_states) { foreach my $row ($ts->rows) { if ($row !~ "disabled"){ my @users = split /,/,$row->[1]; foreach (@users) { print "$row->[0],$_ \n"; } } } }
      and this is working but can these loops simplified....?
        You can get rid of the temporary variable by writing
        for my $row (split /,/, $row->[1]) { ...

        But apart from that it doesn't look bloated, so there's no need to simplify them further.