| | |
Replacing a field separator during a read.
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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: 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 I am learning from a text book and it is not covered.
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
Perl Syntax (Toggle Plain Text)
#!/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";
Last edited by cscgal; Apr 29th, 2006 at 9:22 am.
Look up the substitution operator. s//.
You can replace the data like so:
You can replace the data like so:
Perl Syntax (Toggle Plain Text)
$myvar = "hello|world|yes!"; $myvar =~ s/|/\t/gi; print "$myvar";
Last edited by Comatose; Apr 29th, 2006 at 4:47 pm. Reason: ToothPick Syndrome!
•
•
•
•
Originally Posted by Comatose
Look up the substitution operator. s//.
You can replace the data like so:
Perl Syntax (Toggle Plain Text)
$myvar = "hello|world|yes!"; $myvar =~ s/|/\t/gi; print "$myvar";
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):
Perl Syntax (Toggle Plain Text)
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:
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 Syntax (Toggle Plain Text)
$myvar = "hello|world|yes!"; $myvar =~ s/\|/\t/gi; print "$myvar";
![]() |
Similar Threads
- need help on replacing a value in a field (MSSQL) (MS SQL)
- Address Book Data Base (C++)
- Is the IT/CS field really as, "flooded" as they make it out to be? (IT Professionals' Lounge)
- Output MySQL text field formatted (PHP)
Other Threads in the Perl Forum
- Previous Thread: log rotation perl script
- Next Thread: Help with Perl script to verify IP addresses
Views: 4365 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Perl






