#1 (permalink)  
Old 04-28-2006, 11:05 PM
NevadaSam's Avatar
Registered User
 
Join Date: Apr 2006
Posts: 20
Replacing a field separator during a read.

Replacing a field separator during a read.

I want to replace a pipe symbol (|) field seperator with a tab when I print the contents of records in a file. This is my text file:
Quote:
Jeremiah Stein|LW89W|1|U
Patty Smith|JJ12R|2|W
The cgi script I wrote will read it and print it out with the | between field. How can I replace the | with a tab? This is my script. It can be called up from a link or in the browser's window: http://localhost/cgi-bin/cvtc/c05ex4b.cgi
Quote:
#!/usr/bin/perl
#c05ex4b.cgi - reads data from a text file and creates a dynamic Web page
# that prints registered information
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;

#declare variables
my ($name, $serial, $modnum, $sysletter, @records, $rec);

#Read Records
open(INFILE, "<", "c05ex4.txt")
or die "Error opening c05ex4.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($name, $serial, $modnum, $sysletter) = split(/\|/, $rec);
}

#create Web page
print "<HTML><HEAD><TITLE>Juniper Printer Registrations</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "<B>View Registration File</B></h2><br />\n";
print "The Following Printers have been registratered.<br /><br /> \n";

foreach my $rec(@records) {
print "$rec<br />\n";
}
print "</BODY></HTML>\n";
I am learning from a text book and it is not covered.
Reply With Quote

  #2 (permalink)  
Old 04-29-2006, 11:45 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
I'm sure it probably is covered, you're just not sure what to look for.

I am having a bit of trouble understanding what you mean by "tab". In a text document, there is no such thing as a "tab". Sure, you could put five spaces in there, but it will not be the same as a tab in Word or some other word processor.

Regardless, you want to do some research on "regular expressions" (regex) in Perl. To accomplish what you want, you will need to do something similar to the following:

Code:
$your_variable =~ s/|/'tab'/g;
What this does:
First, you have $your_variable, defined as whatever your output is.
Then, the regular expression searches for the pipe character.
When it finds the pipe character, it will replace it with the word "tab" (I'm positive that there is a perl expression for a non-breaking space, but I'm not sure what that is, so I'm temporarily using the word "tab" to fill that space).
The "g" at the end of the regex makes it a "global" search, which means that it will continue searching/replacing until it reaches the end of the variable.
If you leave off the "g", it will only replace the first instance of the regex, and will then quit.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #3 (permalink)  
Old 04-29-2006, 02:53 PM
NevadaSam's Avatar
Registered User
 
Join Date: Apr 2006
Posts: 20
Thank you. I have this figured out already. I replaced the pipe symble with a tab (\t) while reading the records and used the <pre> tag to get the text to print out the way I wanted it.

I really do appricate you taking the time to read and respond to my post.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
strip carriage returns & linefeed characters from form field amanxman Programming and Scripting 2 03-30-2006 07:52 AM
how to include id# field of db in url? gilgalbiblewhee Programming and Scripting 6 01-05-2006 06:15 PM
Using a variable as defaut value in form field dilbertza2 Programming and Scripting 1 04-17-2005 12:03 PM


All times are GMT -5. The time now is 05:59 PM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter