Replacing a field separator during a read.

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 5
Reputation: NevadaSam is an unknown quantity at this point 
Solved Threads: 0
NevadaSam's Avatar
NevadaSam NevadaSam is offline Offline
Newbie Poster

Replacing a field separator during a read.

 
0
  #1
Apr 29th, 2006
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
  1. #!/usr/bin/perl
  2. #c05ex4b.cgi - reads data from a text file and creates a dynamic Web page
  3. # that prints registered information
  4. print "Content-type: text/html\n\n";
  5. use CGI qw(:standard -debug);
  6. use strict;
  7.  
  8. #declare variables
  9. my ($name, $serial, $modnum, $sysletter, @records, $rec);
  10.  
  11. #Read Records
  12. open(INFILE, "<", "c05ex4.txt")
  13. or die "Error opening c05ex4.txt. $!, stopped";
  14. @records = <INFILE>;
  15. close(INFILE);
  16. foreach my $rec (@records) {
  17. chomp($rec);
  18. ($name, $serial, $modnum, $sysletter) = split(/\|/, $rec);
  19. }
  20.  
  21. #create Web page
  22. print "<HTML><HEAD><TITLE>Juniper Printer Registrations</TITLE></HEAD>\n";
  23. print "<BODY><H2>\n";
  24. print "<B>View Registration File</B></h2><br />\n";
  25. print "The Following Printers have been registratered.<br /><br /> \n";
  26.  
  27. foreach my $rec(@records) {
  28. print "$rec<br />\n";
  29. }
  30. print "</BODY></HTML>\n";
I am learning from a text book and it is not covered.
Last edited by cscgal; Apr 29th, 2006 at 9:22 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Replacing a field separator during a read.

 
0
  #2
Apr 29th, 2006
Look up the substitution operator. s//.

You can replace the data like so:
  1. $myvar = "hello|world|yes!";
  2. $myvar =~ s/|/\t/gi;
  3. print "$myvar";
Last edited by Comatose; Apr 29th, 2006 at 4:47 pm. Reason: ToothPick Syndrome!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: NevadaSam is an unknown quantity at this point 
Solved Threads: 0
NevadaSam's Avatar
NevadaSam NevadaSam is offline Offline
Newbie Poster

Re: Replacing a field separator during a read.

 
0
  #3
Apr 29th, 2006
Thanks for the reply. I have it working now. That is some good information to know as I go along.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Replacing a field separator during a read.

 
0
  #4
Apr 29th, 2006
Cool, do me a favor and up at the top, mark the thread as solved.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Replacing a field separator during a read.

 
0
  #5
Apr 30th, 2006
Originally Posted by Comatose
Look up the substitution operator. s//.

You can replace the data like so:
  1. $myvar = "hello|world|yes!";
  2. $myvar =~ s/|/\t/gi;
  3. 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):

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Replacing a field separator during a read.

 
0
  #6
Apr 30th, 2006
Your absolutely right... I didn't test the code, I went off memory. So, You would need to escape the piping symbol as such:
  1. $myvar = "hello|world|yes!";
  2. $myvar =~ s/\|/\t/gi;
  3. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4365 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC