Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing A Character

Status
Not open for further replies.

hunter13605

Technical User
Apr 7, 2006
44
0
0
US
I am in the middle of writing a pearl program for my High School year book staff that allows them to collect the Senior will and Index's throught the internet to speed up the process. I have a systen setup so that each senior has their own login and then the administrator has a log in that gives him features to add or remove users, reset a users account, and generate reports. Here is the problem.

I am saving the information gathered from the user in a comma seperated form. This causes a problem when the user uses a comma in their text. Is there any way to search each section of text for a comma and replace it with the character set code (,)? Thanks for your help and all the help that you have given me in the past.
 
You could use HTML::Entities

Untested, but I'm thinking something like this:
Code:
$encoded = encode_entities($input, ',');

Let us know your results!

X
 
You should have thought of this before hand. You caould do a search and replace with perl code but it will get all the commas, even the ones used for delimiting the file. You may have to go in and edit the file by hand to replace the commas .
 
you might want to alter the script that creates the csv and instead of commas use | or if you are in love with commas
then add double quotes like "foo","foo,foo","loo" and then split them by ", and then get rid of any other " to get
foo
foo,foo
loo


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I had the same problem with a c++ program that i wrote. It was a program that got a persons name in a standard format and changed it into a directory format. In that case i searched the string of text and relaced the space with an ~. Is there a function that will search a string and replace certain characters. This is a huge school and there will be ofer 200 posts that would need to be corrected. The whole point of the program is to simplify the process for the year book staff. Thanks
 
lets say someone enters this: [red]'whatever ,his name'[/red] as lastname
Code:
$string = $cgi->param('lastname');
$string =~ s/,/~/g;
now your string will look like [red]'whatever ~his name'[/red]
and then you create your csv.

But as I said you can just
Code:
$string = '"'.$cgi->param('lastname').'"';
and then you create your csv.
Now you dont have to worry about that comma in the string.
your csv line will look like this
"foo","moo","whatever ,his name","whatever"


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
My understanding was that people have already entered commas into the flat file and you need to go back and correct that problem. That you will have to do by hand.

On the other hand, if the data is fine, just encode commas as they are sent to the data file and decode them as the data file is read back for display, this is what perluserpengo has showed you how to do above.

And on the other hand if you are coding with perl and don't know how to do a simple character substitution, maybe there is going to be other problems in your script that have yet to surface.
 
The problem that you are all trying to solve is not the problem that i have. I am having the following problem...

If a person typed "I, Joe, ..." then it would save it, when submited, to the .txt file then it thinks the commas in the message are the commas that seperate the variables. Is there a way to replace the commas with the html comma code (,) before saving the data?
 
$text =~ s/,/,/g;


where $text is the data recieved from the form before it's put into the flat file. Reverse to operation when reading from the file.
 
Thanks a lot. Should i put this in right before it is saved?
 
obviously it has to be done before the data is saved to disk, but when is up to you, it depends on your program.
 
Alternate solution: use Text::CSV to create the file, and read it back later. It handles all the bad stuff for you.


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
this code from kevin worked great.

Code:
$text =~ s/,/,/g;
 
Example:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new();

my @stuff = ("good stuff", qq{bad,,, "karma",, for CSV, users});

$csv->combine(@stuff);
my $line = $csv->string();

open(CSV, ">my.csv") or die $!;
print CSV "$line\n";
close(CSV);

open(CSV, "my.csv") or die $!;
$line = <CSV>;
chomp($line);

$csv->parse($line);
print join("\n", $csv->fields());

close(CSV);
note that Text::CSV it handles the embedded commas AND the embedded quotes properly. It's quite a cool module (in a geek definition of cool, obviously) if you have any CSV file manipulation to do...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top