Gossamer Forum
Home : General : Perl Programming :

join field error

Quote Reply
join field error
Hi

I am using the following code to join a field and text to construct a URL.
Quote:
$URL = join("http://www.mysite.com",$field,".html?d=1&t=de");

Unfortunately I'm getting the result:
Quote:
fieldhttp://www.mysite.com.html?d=1&t=de

Can anyone see what I'm doing wrong?

Thanks
Quote Reply
Re: [Alba] join field error In reply to
It looks to me like you are trying to use 'join' to concatenate a group of fields together in the given order. While 'join' can be used to do that, in this particular case, it seems rather silly.

join functions like this:
Code:
sub join ($@) {
my ($expr, @list) = @_;
local $" = $expr;
return "@list";
}


or

Code:
sub join ($@) {
my ($expr, @list) = @_;
my $return = '';
for (my $i = 0; $i < @list; $i++) {
$return .= $list[$i];
if ($i + 1 < @list) {
$return .= $expr;
}
}
return $return;
}


However, you probably want
Code:
$URL = join('', 'http://www.mysite.com', $field, '.html?d=1&amp;t=de');

which is the same as (although, probably less efficient than)

Code:
$URL = 'http://www.mysite.com' . $field . '.html?d=1&amp;t=de';

which can also be written as (actually, the perl interpreter converts the follow to the previous during parsing)

Code:
$URL = "http://www.mysite.com$field.html?d=1&amp;t=de";



If this is not what you want, it would help to have an example '$field' and know what the expected output is.
Quote Reply
Re: [mkp] join field error In reply to
Thanks, I used
Quote:
$URL = 'http://www.mysite.com' . $field . '.html?d=1&amp;t=de';