Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    groan: changing array separation.

    Hi,

    I must be suffering memory drop or maybe I need a ram upgrade.

    I am calling in data from a sub and using it to build an array. when it comes in from the sub I need to surround it with single quotes ( ' ) and make the separator a ',' like this

    'var','another_var','yet_another'

    I thought what I have tried would do it but I am mis recalling the correct way and I can't find where I used it before. I mayu have deleted it or lost it in a (unwelcome) windows re-install.

    here's the main code:
    Code:
    my $returned_data = get_approved_urls();
      my @valid = @$returned_data;
      my $tempvar = join(" ',' ",@valid);
      my @var_now = @$tempvar;
      @var_now = " {'@var_now'} ";
    (I tried with and without brackets)
    where am I getting it wrong?

    bazz
    Last edited by bazz; 05-24-2009 at 04:47 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,836
    Thanks
    2
    Thanked 158 Times in 153 Posts
    See if this helps.
    Code:
    my $returned_data = [ 'one', 'two', 'three', 'four' ];
    
    s/^|$/'/g for @$returned_data;
    
    print join(',', @$returned_data);

  • #3
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    There is no way to quote an array, when you do it becomes a string. Personally I think you are confused about something but I don't know what.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks for the replies.

    I shall ponder it while I try something else.

    @Kevin: the array is currently declared in the script as
    Code:
    my @valid = ( 'example.com','example2.com');
    I am trying to change the source of the array, to be my db.

    I'll think it through some more.


    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,836
    Thanks
    2
    Thanked 158 Times in 153 Posts
    I am trying to change the source of the array, to be my db.
    I'm not sure what you mean by that, but if you want to give us more details, I'm sure we can show you what you need to do.

  • #6
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks FishMonger.

    Curently my script has the array declared within it and it get the values for it, from a flat file. No longer need to do it that way since I moved over to a mysql db

    So I want to get the data from the db and build the array automatically.

    heres the full script.

    Code:
    #!/usr/bin/perl -w
    
      use CGI ':standard';
      use CGI::Carp qw(fatalsToBrowser);
      use warnings;
      use DBI;
      use strict;
      my $cgi = new CGI;
      my $referer = $ENV{'HTTP_REFERER'};
      my $client = param('client');
      my $img = param('img');
      my $size = param('size');
      my $connect = connect_to_bookings_db();
    
      
      print $cgi->header;
    
      my $valid = find_authorisedURLS();
      s/^|$/'/g for @$valid;
    
      my @valid = join(',', @$valid);
      check_url_referer(@valid);
    
    
      my $image_path = '/var/www/vhosts/domain/subdomains/cms/httpdocs/directory/';
    
    
    
      my $path_to_img = $image_path . '/' . $client. '/' . $size . '/'. $img;
    
      my $f;
      print "Content-type: image/jpg\n\n";
      open IMG, $path_to_img;
      while (read IMG, $f, 16384) {
        print $f;
      }
      close IMG;
    
    
    sub check_url_referer {
       my $good_ref;
       my $referral_cnt = @valid;
       if ($referral_cnt > 0) {
    
          foreach my $referer (@valid) {
               if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
                  $good_ref = "yes";
                last;
               }
          }
          if ($good_ref ne "yes") {
             &go_away;
          }
       }
    }
    
    
    
    sub go_away {
    print qq( unauthorised url );
    exit;
    }
    
    
    
    
    sub find_authorisedURLS {
    
      my $sth = $connect->prepare ("SELECT distinct
                                           c.web_url
                                      from contact c
                                     where c.web_url != 'null'
                                     and c.web_url != ''
                                     and c.web_url != ' '
                                     and c.web_url != 'NULL'
                                      
                                   ") or die "prepare statement failed: $DBI::errstr\n";
    
      
      my @returned_fields;
      my $tempvar;
      my @var_to_return;
      my ($domain, $stuff);
      my @valid;
      $sth->execute() or die "execute statement failed : $DBI::errstr\n";
    
      while ( my(@fields) = $sth->fetchrow_array() ) 
      {
        if ($fields[0] =~/[\w\d]+/i && $fields[0] ne '') 
        {
        $fields[0] =~ s/http\:\/\///; # strip http
        }
      $fields[0] =~ s/www\.//;
      push (@valid, $fields[0]);
      }
      
      
    return @valid;
    
    }
    
    
    
    sub connect_to_bookings_db {
    
      my $db		= '*******';
      my $srv		= '********';
      my $user		= '*******';
      my $pass		= '**********';
      my $port		= '****';
      #$user		= shift;
      my $dbh = DBI->connect("DBI:mysql:$db:$srv", $user, $pass,
                            {'RaiseError' => 1, 'PrintError' => 0 })
                or die "Connection Failed: $db DB on $srv\n\t$DBI::errstr\n";
    return $dbh;
    }
    The error is

    Can't use string ("73") as an ARRAY ref while "strict refs" in use at mask.pl line 22.


    This is an update to an older script which is to stop hotlinking of images and to hide where they are stored.

    The array works if it is :

    my @valid = ('domain.com','domainTwo.com');

    bazz
    Last edited by bazz; 05-24-2009 at 04:24 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #7
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    By the way that now working. when I added print $cgi->header; it all became much clearer.

    Thanks very much for your time.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,836
    Thanks
    2
    Thanked 158 Times in 153 Posts
    A couple comments/suggestions.

    Don't use the -w switch, unless it's a 1 liner command. The warnings pragma that you're already using is better.

    Why load the CGI ':standard' functions when you plan on and actually use the OO methods?

    Don't use the indirect object when creating objects.
    Meaning, instead of this:
    my $cgi = new CGI;

    do this:
    my $cgi = CGI->new;

    my @valid = join(',', @$valid); creates a single element array, so why not just put it in a scalar?

    ALWAYS check the return code of an open call and it's preferable to to use a lexical var for the filehandle as well as the 3 arg form of open.

    Within a sub you almost never want to use a global var directly (I'm referring to @array). Instead, you should pass it to the sub either by reference or by value depending on what you need to do with its values.

    Don't use the '&' when calling a sub unless you know, understand, and want its side effects.

    Since your select statement is only retrieving 1 field, it would be cleaner to put that into a scalar instead of a 1 element array.

    ----

    Hmm, I meant to only point out 1 or 2 items, did I go overboard?

  • #9
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Much appreciated FishMonger.

    I have to review the code anyway because usually (always), I can make it more efficient somewhere, once I get it to work.

    Any shortcomings are better mentioned.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •