i'm trying to split values that i'm getting using Web::Scraper. However, i seem to be overwriting my hash? I'm not sure how this can be. Following is my code:
#!/usr/bin/perl use strict; use warnings; use Web::Scraper; use Data::Dumper::Simple; my( $infile ) = $ARGV[ 0 ] =~ m/^([\ A-Z0-9_.-]+)$/ig; my $page = scraper { process '//*/div[@id="Results"]/table/tr/td', 'table[]' => scraper +{ process '//span', 'name' => '@id', 'attr' => '@title'; }; process '//*//table[@id="Documents"]/tr', 'docs[]' => scraper { process '//tr', 'attr' => '@title'; }; }; open(FILE, "< $infile" ); my $content = do { local $/; <FILE> }; my $res = $page->scrape( $content ) or die "Can't define content to parser $!"; # print Dumper( $res ); my %values; for my $data ( @{$res->{ table } } ) { next unless $data->{ name } and $data->{ attr }; foreach my $line (split /\n/, $data->{ attr } ) { %values = split /:/, $line; } print "$data->{ name }\t $data->{ attr }\n\n" ; } print Dumper( %values ); #print $content;
the output that i'm looking to work with is:
Name: value Name2: value2 etc
also, this is only my second option. the author of the Web::Scraper package gives an example of adding functionality in his function, but i don't understand how to return the data. his example is:
my $scraper = scraper { process 'a[rel~="tag"]', 'tags[]' => sub { my $uri = URI->new($_->attr('href')); my $label = (grep length, split '/', $uri->path)[-1]; $label =~ s/\+/%20/g; uri_unescape($label); }; };
In reply to loop split into a hash by ag4ve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |