LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2007, 02:44 PM   #1
arash8m
LQ Newbie
 
Registered: May 2007
Posts: 19

Rep: Reputation: 0
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.
 
Old 07-12-2007, 05:06 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Last edited by wjevans_7d1@yahoo.co; 07-12-2007 at 05:08 PM.
 
Old 07-13-2007, 01:50 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
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?
 
Old 07-13-2007, 11:56 AM   #4
arash8m
LQ Newbie
 
Registered: May 2007
Posts: 19

Original Poster
Rep: Reputation: 0
thanks for your replies. it's working fine now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions, Perl, everything but a / xemous Programming 6 05-22-2006 11:09 AM
Perl regular expressions CyberJedi Programming 2 10-02-2005 12:17 PM
Perl regular expressions daYz Programming 6 08-11-2005 08:27 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 12:32 PM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 06:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration