in reply to Recover Excel data from cells with Foreach loop

What is going wrong please?

This ignores the spreadsheet and just gets 0 to 9

foreach my $data (0..9){ $driver->get($data);

Try

#!/usr/bin/perl use strict; use Spreadsheet::Read; my $excel = ReadData("TEST.csv"); my $row = 1; my $url = $excel->[1]{'A'.$row}; while ($url){ # do something with url process($url); ++$row; $url = $excel->[1]{'A'.$row}; } sub process { my ($url) = @_; print "Processing $url\n"; # selenium code etc }
poj

Replies are listed 'Best First'.
Re^2: Recover Excel data from cells with Foreach loop
by Perlchaoui (Sexton) on Nov 10, 2018 at 20:57 UTC

    Thank you Poj ! But why is it ignoring the spreadsheet?

    I've checked and it's working fine, thx ! Otherwise, is there a way to do it with a loop, "foreach" or "for"?!

    Thanks again

      see foreach loops in perlsyn. Your foreach loop iterates over the list values of 0 to 9 and sets the scalar variable $data to be each element of the list in turn. Because the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop.

      Try

      #!/usr/bin/perl use strict; use Spreadsheet::Read; my $excel = ReadData("TEST.csv"); my $maxrow = $excel->[1]{maxrow}; for my $row (1 .. $maxrow){ my $url = $excel->[1]{'A'.$row}; # do something with url process($url); } sub process { my ($url) = @_; print "Processing $url\n"; }
      poj

        Hmmm ; I understand better now. Thx. I need to work more with the foreach loop

        Further to that i have a latest remark Poj. I would like to make a screenshot once an url is opened; As i am in a loop, i would like to store for each url a screnshot file. I did quickly as follows. I got no error but i got no screenshot :)

        I used your code

        sub process { my ($url) = @_; print "Processing $url\n"; # selenium code etc $driver->get($url); $driver->maximize_window(); $driver->pause(2000); #my $filename = "screenshot.png"; #$driver->capture_screenshot($filename); my $path = "C:/Users/user1/Documents/TESTPERL"; $driver->capture_screenshot("$path/Website-$url.png"); $driver->pause(2000); }

        Many thanks Poj