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

Dear Monks,


I crave your benevolence for this error.

I have already had some help on getting to this stage with this code but I'm coming a cropper on slitting the trailing null bytes from the generated list.

The code uses CGI::Vars which I believe is causing the trailing \0 in the generated list. The code is designed to put the $column string into the SELECT part of the SQL query as part of moving the whole thing from ODBC to DBI. When the code is run without any 'split' it outputs the lists with trailing bytes but if I uncomment my split I get a numeric return which I assume is the number of variables returned which doesn't help whilst trying to build a list of columns. $fd is the params part of vars() and the @columns code is working.

my @columns = join ',', map { $db->quote($fd{$_}) } grep { exists $fd{$_}} ('chk'); my $columns = split/\0/, @columns; SQL code SELECT $column

I'd be grateful for some pointers as to what I have done incorrectly so that I can learn from this and get it right in the future.

Thanks

Replies are listed 'Best First'.
Re: Splitting \0
by japhy (Canon) on Jun 01, 2007 at 11:26 UTC
    I think you've got your '@' and '$' in the wrong places. join() returns a SCALAR, split() returns a LIST (or a scalar, but I think you want the list here).
    my @column_names = qw( chk ); # I assume you have more? my $columns_joined = join "," => map { $db->quote($fd{$_}) } grep { exists $fd{$_} } @column_names; my @column_values = split /\0/, $columns_joined;

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Hi Japhy, No the SELECT is still returning 1 or nothing.
Re: Splitting \0
by holli (Abbot) on Jun 01, 2007 at 11:14 UTC
    Can you please post a dump of $fd?
    use Data::Dumper; ... print Dumper($fd);


    holli, /regexed monk/

      With the split uncommented:

      SELECT 1 FROM test.lists WHERE Listname

      With split commented:

      SELECT 'DateCreated\0DateDeleted\0DateRenamed\0OldListname\0DateChecked' FROM test.lists

      If I print $fd I get:

      $VAR1=undef

      As per above, any help so I can learn from a practical example gratefully appreciated

        Where/when did you print Dumper($fd)? You say it is undef, but as i read, it must contain some data. FunkyMonk is right, it must read print Dumper(\%fd). Could you please put that just before the loop construct?
        And if it's true that you used print Dumper($fd), it shows you didn't use strict; and you will have to face the inqusitors :-)


        holli, /regexed monk/
Re: Splitting \0
by Quicksilver (Scribe) on Jun 01, 2007 at 14:49 UTC
    Thanks Holli, FunkyMonk and Japhy for all your help. I've finally got it to split correctly by changing the ('chk') which was not pulling all of my columns through correctly into the join and split functions. I have also realised there is a second way of getting this function formatted which may be easier to maintain via the \%fd dump so TMTOWDI! However both ways are useful for another planned script.