Re: Big problem with @array and Chomp ... I think :o
From: Kevin Shay (kevin_shay_at_yahoo.com)
Date: 11/05/03
- Next message: Ilya Zakharevich: "Re: Rounding a float in Perl?"
- Previous message: Jürgen Exner: "Re: printing characters"
- In reply to: Robert TV: "Big problem with @array and Chomp ... I think :o"
- Next in thread: Robert TV: "Re: Big problem with @array and Chomp ... I think :o"
- Reply: Robert TV: "Re: Big problem with @array and Chomp ... I think :o"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 4 Nov 2003 19:48:29 -0800
"Robert TV" <ducott_99@yahoo.com> wrote in message
news:<j0Tpb.290847$6C4.168943@pd7tw1no>...
> @recipients = CGI::param('csvdata');
>
> open (FILE,">>$activeuser/csvdump.txt");
> close(FILE);
>
> open (ADDRESSES, ">$activeuser/csvdump.txt") or die "Can't open file: $!";
> print ADDRESSES @recipients;
> close(ADDRESSES);
>
> open (ADDRESSES, "<$activeuser/csvdump.txt") or die "Can't open file: $!";
> @recipients = <ADDRESSES>;
> close(ADDRESSES);
>
> unlink <$activeuser/csvdump.txt>;
>
> <-- End Code Example -->
>
> Ok, right about now your wondering why did I take the form contents, write
> it to a temp file, then open and get the data back from the temp file. For
> some reason I cannot get the code to work any other way.
The problem may lie in the first line of code above:
@recipients = CGI::param('csvdata');
It sounds like you have a single HTML form field called csvdata, into
which the user enters multiple lines of text. If that's the case, the
CGI module will not automatically turn this into a list. It's a single
value. So the entire set of data is being placed into the first
element of @recipients.
When you print that one-element array to a file, it simply prints that
one element; the element happens to have multiple lines, so the
resulting file will have multiple lines. Then, when you re-open the
file and do this:
@recipients = <ADDRESSES>;
Perl reads the lines of the file into the array, one line per element,
which is what you wanted in the first place. So you could probably
just do this to avoid the whole temp file workaround:
@recipients = split(/\n/, CGI::param('csvdata'));
As for the actual chomp problem, it may have something to do with the
way different operating systems encode newlines differently (\n on
Unix, \r\n on DOS/Windows, \r on Mac). Depending on which platform the
user's browser is running on, you may not be getting just \n
characters separating the lines. So you might want to normalize
everything to \n newlines:
my $csvdata = CGI::param('csvdata');
$csvdata =~ s/\r\n?/\n/g;
@recipients = split(/\n/, $csvdata);
Kevin
--
perl -MLWP::UserAgent -e '$u=new LWP::UserAgent;$u->agent("japh");
print join(" ",(split(/\s+/,(split/\n/,$u->request(HTTP::Request->
new(GET=>join("",split(/\n/,"http://groups.google.com/groups?selm=
4365%40omepd.UUCP&output=gplain"))))->content)[60]))[0..3]),",\n"'
- Next message: Ilya Zakharevich: "Re: Rounding a float in Perl?"
- Previous message: Jürgen Exner: "Re: printing characters"
- In reply to: Robert TV: "Big problem with @array and Chomp ... I think :o"
- Next in thread: Robert TV: "Re: Big problem with @array and Chomp ... I think :o"
- Reply: Robert TV: "Re: Big problem with @array and Chomp ... I think :o"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|