#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    43
    Rep Power
    7

    Wget with array?


    Hi,

    I have a cgi perl script and I'm trying to use wget with an array in the address, but it is not interpreting the array. I checked the logs and it executing the wget with http://someaddress=@array[$i] instead of "http://someaddress=111,222,333,444,555.

    Code:
    @array[0] = 111,222,333,444,555;
    
    for ($i = 0; $i<$as; ++$i){
    system ('wget "http://someaddress=@array[$i]"');
    }
    if i hard code the values in it works fine but that is not an option.

    Im still very new to perl, so if anyone could help it would be greatly appreciated.

    Thanks!!!
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,259
    Rep Power
    1810
    There are a couple of problems. Your syntax for creating an array is incorrect. The other problem has to do with quoting strings.

    Perl has several constructs for quoting and building strings. When you use single quotes on the outer part of a string, you are telling perl to use the string literally. No variables will be interpolated, so that's why you saw what you did. You need outer double quotes, or use the qq// notation:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;
    
    my @array = (111,222,333,444,555);
    
    print Dumper \@array;
    
    foreach my $parameter (@array){
    	my $command_string = qq{wget "http://someaddress=$parameter"};
    	print "$command_string\n";
    	system ($command_string);
    }
    Also worth noting that perl has the LWP library to make web requests directly from perl. No need to use the system or the wget command.

    Comments on this post

    • kochaloch agrees : Thanks

IMN logo majestic logo threadwatch logo seochat tools logo