Odd string concatenation behavior?

Reply

Join Date: Jan 2007
Posts: 1
Reputation: jemather is an unknown quantity at this point 
Solved Threads: 0
jemather jemather is offline Offline
Newbie Poster

Odd string concatenation behavior?

 
0
  #1
Jan 22nd, 2007
I am quite new to Perl, and have not had much luck finding a cause for some seemingly-strange behavior of a Perl script I am playing around with. I am running Perl 5.8.6 on OS X 10.4.8. I have done some perusing, but I'm not quite sure how to describe what I'm seeing so I haven't had much luck.

  1. $text = "JEZEBEL (Disc, 1938)";
  2. my @temp = split (/\(/, $text);
  3. $title = substr(@temp[0], 0, - 1);
  4. my @temp2 = split (/,/, @temp[1]);
  5. $format = substr(@temp2[0], 0);
  6. $r_date = substr(@temp2[1], 1, - 1);
  7.  
  8. my $sql = "INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES ('".$title1."', '".$format1."', ".$r_date1.")";
  9. print $sql;

This code gives the following output.

  1. INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES ('JEZEBEL', '', )INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES ('', 'Disc', 1938)

I cannot understand why the output becomes seperated like this, with one variable inserted properly into the first statement and the other two into the second - and its not something that is happening to the output in my terminal, as I can see the same problem in my database when I run it as an SQL statement. When I look at each variable individually it seems to be correct, but when I try to concatenate them I always seem to end up with two statements like this. Any thoughts on why this is occuring? Could it be that the second two variables are some type of array? Thanks a lot for the help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Odd string concatenation behavior?

 
0
  #2
Jan 22nd, 2007
I think what might be wrong here is that Perl treats both single and double quotes as string terminators, whether you started the string with a single or double quote. My advice is try escaping the single quotes in your string with back slashes ( \ ). So the line in question should be:

  1. my $sql = "INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES (\'" . $title1 . "\', \'" . $format1 . "\', ".$r_date1."\')";

I might be wrong but it's worth a try .

Steven.
Last edited by Mushy-pea; Jan 22nd, 2007 at 4:23 pm.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Odd string concatenation behavior?

 
0
  #3
Jan 22nd, 2007
jemather,

the code you posted shouldn't even produce values for the scalars in the $sql scalar.

  1. ('".$title1."', '".$format1."', ".$r_date1.")"
  2.  

there are no $title1 or $format1 or $date1 scalars defined in the code you posted. You have $title and $format and $r_date.

with the correct scalars, the line is better written without using any concatenation at all:

  1. my $sql = "INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES ('$title', '$format', $r_date)";
Last edited by KevinADC; Jan 22nd, 2007 at 4:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Odd string concatenation behavior?

 
0
  #4
Jan 22nd, 2007
Steven,

single-quotes inside of double-quoted strings have no special meaning. No need to escape them.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Odd string concatenation behavior?

 
0
  #5
Jan 22nd, 2007
if $text is always in that same format:

  1. $text = "JEZEBEL (Disc, 1938)";
  2. my ($title,$format,$r_date) = $text =~ /^(\S+)\s*\(([^,]+),\s*(\d+)\)/;
  3. my $sql = "INSERT INTO movies (TITLE, MEDIA, YEAR) VALUES ('$title', '$format', $r_date)";
  4. print $sql;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC