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:

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

#!/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.

Look up the substitution operator. s//.

You can replace the data like so:

$myvar = "hello|world|yes!";
$myvar =~ s/|/\t/gi;
print "$myvar";

Thanks for the reply. I have it working now. That is some good information to know as I go along.

Cool, do me a favor and up at the top, mark the thread as solved.

Look up the substitution operator. s//.

You can replace the data like so:

$myvar = "hello|world|yes!";
$myvar =~ s/|/\t/gi;
print "$myvar";

I'm wondering if you or the OP ran the code you suggested, I think you will find the results not what is expected if the code is left as-is.

Usually you don't want to do a substitution on field delimiters but I guess you could if you wanted to. You split the string on the delimiters then munge the data, I had already suggested using join for this (on another forum):

print join("\t",split(/\|/,$var));

Nit-pik: There is also no need to use the "i" option to substitute non alpha characters as there is no case involved.

Your absolutely right... I didn't test the code, I went off memory. So, You would need to escape the piping symbol as such:

$myvar = "hello|world|yes!";
$myvar =~ s/\|/\t/gi;
print "$myvar";

And as for the regex optional modifier's Whoopie Do. You can add it or leave it, I don't think it really matters. I think you'll find regex to be faster in operation (not that it would be noticable on small amounts of data) than nested splits and joins, but I guess it boils down to style.

perl -pi.int -e 's/\|/,/g;' fileName.ext

But I am getting

'/' is not recognized as an internal or external command, operable program or batch file.

Any idea?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.