LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   perl regular expressions (http://www.linuxquestions.org/questions/programming-9/perl-regular-expressions-568811/)

arash8m 07-12-2007 01:44 PM

perl regular expressions
 
Hi all,

I'm writing a CGI in which I get some data from a database and then display it using xml. The problem is that let's say the data is "data1" and "data2". I save these two values into an array, but there's an extra character saved with them as well. If I display the array using html, this is the result:

data1?
data2?

If I just run the perl/cgi script in linux, the output is correct, which is:

data1
data2

If I run the perl/cgi script in linux and save the output to a text file this is what will appear in the file:

data1^@
data2^@

I tried using the regex $data1=~s{[\W\_D]}{}g, but it removes all the spaces between the words as well and I don't want that.

Any help is appreciated.

wjevans_7d1@yahoo.co 07-12-2007 04:06 PM

The ^@ suggests that the offending character is a null character; that is, all bits are off.

But even though NUL characters present a rather unusual case for regular expressions, it's not the case that all bets are off, because you can get rid of null characters in a string thus:

Code:

$data99=~s/\000//g;
So if my guess is right, that will get rid of the symptom.

You can check whether my guess is right by running this Perl fragment on your string (before removing the NUL character):

Code:

@up=unpack("C*",$troublesome_string);

for($jndex=0;
    $jndex<0+@up;
    $jndex++
  )
{
  print(sprintf("%02X",$up[$jndex]));

  if(($up[$jndex]>=0x20) &&
    ($up[$jndex]<=0x7E)
    )
  {
    printf(sprintf(" %c",$up[$jndex]));
  }

  printf("\n");
}

... and you may want to do that, because I'm sure that this code is important enough to you not only to get rid of the symptom, but remove the underlying cause of the problem. Otherwise, that problem may come back to bite you in worse ways than just showing a quirky character on the browser's screen.

Hope this helps.

chrism01 07-13-2007 12:50 AM

I'd sanity check the data input code. That looks like a ctrl char eg newline (\n) or something like that.
What do you get if you use interactive SQL to do an SQL like
SELECT concat('X', data-col, 'X')
or equiv; ie try to concatenate an X against each end of the data val?

arash8m 07-13-2007 10:56 AM

thanks for your replies. it's working fine now.


All times are GMT -5. The time now is 08:53 PM.