Problem with join function (and split function) 
Author Message
 Problem with join function (and split function)

In a script I have tried to take an array and join its elements into one
string delimited by colons. This doesn't seem to be working.
The line in question is line 95 and the script is
http://www.*-*-*.com/
inspection at http://www.*-*-*.com/
Through debugging I have established that it is an array before and after
using join on it.

It doesn't actually affect the program as later the line which should
split the string into an array (using colon as the delimiter) passes it
straight through. (line 136)

As I say this means the script functions fine but I would like to know
more about these two anomolies.

Thanks

--
Geoff Soper

Take a look at the Soundhouse page http://www.*-*-*.com/



Sat, 01 Mar 2003 07:24:30 GMT  
 Problem with join function (and split function)

Quote:
>In a script I have tried to take an array and join its elements into one
>string delimited by colons. This doesn't seem to be working.
>The line in question is line 95 and the script is
>http://www.soundhouse.co.uk/cgi-bin/2.cgi which is available for
>inspection at http://www.soundhouse.co.uk/geoff as is the database file.
>Through debugging I have established that it is an array before and after
>using join on it.

>It doesn't actually affect the program as later the line which should
>split the string into an array (using colon as the delimiter) passes it
>straight through. (line 136)

>As I say this means the script functions fine but I would like to know
>more about these two anomolies.

in future please post a short code example showing the problem you're
having .. people here appreciate not having to go to your web site to
look at the code

the problem in your code starts with this line

  $form_data{$_} = [param($_)] for param();

which is storing an array REFERENCE into the %form_data hash .. it's not
storing an array .. therefore when you do the join later on the second
argument to the join is actually an array reference - where it should be
an array (or more accurately a list)

an array reference is only one list element .. so there's nothing to
'join' with .. and you get back the array reference

you need to either store the array itself in the %form_data hash .. or
dereference the arrayref in the 'join' call like this


NB: this confusion between an array and an arrayref is probably causing
problems elsewhere in your code

--



Sat, 01 Mar 2003 09:50:06 GMT  
 Problem with join function (and split function)

Quote:


> >In a script I have tried to take an array and join its elements into
> >one string delimited by colons. This doesn't seem to be working. The
> >line in question is line 95 and the script is
> >http://www.soundhouse.co.uk/cgi-bin/2.cgi which is available for
> >inspection at http://www.soundhouse.co.uk/geoff as is the database
> >file. Through debugging I have established that it is an array before
> >and after using join on it.

> in future please post a short code example showing the problem you're
> having .. people here appreciate not having to go to your web site to
> look at the code

I'm always hesitant about posting large pieces of code but I will post
snippets in the future

Quote:
> the problem in your code starts with this line
>   $form_data{$_} = [param($_)] for param();
> which is storing an array REFERENCE into the %form_data hash .. it's not
> storing an array .. therefore when you do the join later on the second
> argument to the join is actually an array reference - where it should be
> an array (or more accurately a list)

OK I'll take your word for it, that doesn't mean a huge amount to me! That
piece of code was actually suggested to me by somebody else:

On 10 Sep in comp.lang.perl.misc, Randal L. Schwartz

Quote:

> If you have a "select multiple", then that code returns the *first*
> selected item, not all of them.  A more general capture will be to make
> a multiple-hits-safe structure, as:
>     use CGI qw(param);
>     my %data;
>     $data{$_} = [param($_)] for param();
> Now $data{"bigfield"}[0] is the first (possibly only) value for
> "bigfield".

***** end quote ******

This lead me to think that $data{"bigfield"} would therefore be an array,
you're saying that it's a reference?

Quote:
> an array reference is only one list element .. so there's nothing to
> 'join' with .. and you get back the array reference
> you need to either store the array itself in the %form_data hash .. or
> dereference the arrayref in the 'join' call like this

I think I really need to fix this at the source and store the array in the
%form_data hash instead of trying to fix it later in the code.

Quote:

> NB: this confusion between an array and an arrayref is probably causing
> problems elsewhere in your code

Great :-)

Thanks for your time and help

--
Geoff Soper

Take a look at the Soundhouse page http://www.soundhouse.co.uk/



Sat, 01 Mar 2003 16:00:02 GMT  
 Problem with join function (and split function)

Quote:


-
>> you need to either store the array itself in the %form_data hash .. or
>> dereference the arrayref in the 'join' call like this

>I think I really need to fix this at the source and store the array in the
>%form_data hash instead of trying to fix it later in the code.

I don't know where my head was .. (as others will almost certainly pull
me up on) you cannot store the entire array in the one element .. you
have to store the array reference (as Randal showed)

which means that you have to use the following syntax later on to get at
the array

Quote:

>> NB: this confusion between an array and an arrayref is probably causing
>> problems elsewhere in your code

>Great :-)

take some time out and read about references in the perlref section of
the documentation .. type the following at a command prompt

  perldoc perlref

what you're dealing with here is explained in point (2) - a reference to
an anonymous array .. it's worthwhile learning it - because there's a
lot in Perl that you cannot do without references

--



Sat, 01 Mar 2003 16:13:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to use split function to split on a backslash

2. Problem in 'split' function

3. Problem with split function

4. Problem with split function.

5. Question Of Leading \n From Join Function.

6. Perl join function

7. using split function twice on same line

8. Split function

9. Using SPLIT function with a Period

10. Using a period as a delimiter in the split() function

11. split function that handles quoting..

12. perlcc does not compile when split to array function is used

 

 
Powered by phpBB® Forum Software