Home > Archive > PERL Beginners > September 2006 > process data into an array using CGI script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
process data into an array using CGI script
|
|
| Chen Li 2006-09-02, 9:57 pm |
| Dear all,
I paste some data into textarea in a CGI script and
use param('data')to retrieve the data. I want to pass
the data into an array but what I find is that I only
one dimensional array. Can someone here give me a
hand?
Thanks,
Li
data pasted into textarea:
1 1 1
2 2 2
3 3 3
(each column separated by tab and each row ended with
\n)
after passed into script and processed the expected
result is
my @data=(
[1,1,1],
[2,2,2],
[3,3,3],
);
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| Jeff Pang 2006-09-02, 9:57 pm |
| >
>data pasted into textarea:
>
>1 1 1
>2 2 2
>3 3 3
>
>(each column separated by tab and each row ended with
>\n)
>
>after passed into script and processed the expected
>result is
>my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
>
Hello,
You can do it like:
my $data = $q->param('data'); # here $q is a CGI objective
my @data = map { [split] } split/\n/,$data;
Hope this helps.
--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
| |
| Peter Scott 2006-09-02, 9:57 pm |
| On Sat, 02 Sep 2006 18:09:10 -0700, chen li wrote:
> Dear all,
>
> I paste some data into textarea in a CGI script and
> use param('data')to retrieve the data. I want to pass
> the data into an array but what I find is that I only
> one dimensional array. Can someone here give me a
> hand?
>
> data pasted into textarea:
>
> 1 1 1
> 2 2 2
> 3 3 3
>
> (each column separated by tab and each row ended with
> \n)
>
> after passed into script and processed the expected
> result is
> my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
my @data = map [ split ] => split /\r?\n/, param('data');
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
| |
| DJ Stunks 2006-09-02, 9:57 pm |
| Chen Li wrote:
> Dear all,
>
> I paste some data into textarea in a CGI script and
> use param('data')to retrieve the data. I want to pass
> the data into an array but what I find is that I only
> one dimensional array. Can someone here give me a
> hand?
>
> Thanks,
>
> Li
>
> data pasted into textarea:
>
> 1 1 1
> 2 2 2
> 3 3 3
>
> (each column separated by tab and each row ended with
> \n)
>
> after passed into script and processed the expected
> result is
> my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
#!/usr/bin/perl
use strict;
use warnings;
my $textarea = do { local $/; <DATA> };
my @data = map [ split ], split /\n/, $textarea;
print "\@data = (\n";
for my $i (@data) {
print ' [', join(', ',@$i), "]\n";
}
print ");";
__DATA__
1 1 1
2 2 2
3 3 3
-jp
| |
| Paul Lalli 2006-09-02, 9:57 pm |
| Chen Li wrote:
> I paste some data into textarea in a CGI script and
> use param('data')to retrieve the data. I want to pass
> the data into an array but what I find is that I only
> one dimensional array. Can someone here give me a
> hand?
>
> Thanks,
>
> Li
>
> data pasted into textarea:
>
> 1 1 1
> 2 2 2
> 3 3 3
>
> (each column separated by tab and each row ended with
> \n)
>
> after passed into script and processed the expected
> result is
> my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
>
I could be wrong, but isn't this the same question you've asked twice
already now?
http://groups.google.com/group/perl...18852c0bfd697e3
http://groups.google.com/group/perl...ae07f14704aa334
| |
| Mumia W. 2006-09-03, 3:57 am |
| On 09/02/2006 08:09 PM, chen li wrote:
> Dear all,
>
> I paste some data into textarea in a CGI script and
> use param('data')to retrieve the data. I want to pass
> the data into an array but what I find is that I only
> one dimensional array. Can someone here give me a
> hand?
>
> Thanks,
>
> Li
>
> data pasted into textarea:
>
> 1 1 1
> 2 2 2
> 3 3 3
>
> (each column separated by tab and each row ended with
> \n)
>
> after passed into script and processed the expected
> result is
> my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
>
>
"Perldoc perllol" shows you how to create lists of lists
(arrays of arrays).
| |
| xaviermasr@ya.com 2006-09-03, 3:57 am |
| A Diumenge 03 Setembre 2006 03:09, chen li va escriure:
> Dear all,
>
> I paste some data into textarea in a CGI script and
> use param('data')to retrieve the data. I want to pass
> the data into an array but what I find is that I only
> one dimensional array. Can someone here give me a
> hand?
>
> Thanks,
>
> Li
>
> data pasted into textarea:
>
> 1 1 1
> 2 2 2
> 3 3 3
>
> (each column separated by tab and each row ended with
> \n)
>
> after passed into script and processed the expected
> result is
> my @data=(
> [1,1,1],
> [2,2,2],
> [3,3,3],
> );
>
>
> ________________________________________
__________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
Sure, chomp the elements, then you put a delimiter before and after each entry
ans use split function to separet those elements. Finally, assign them into
an array so each element be one of your CGI data elements.
The sintax of split operator is: @fields = split /separator/, $string
(Ref: "Learning Perl, 4th. edition, Ed. O'Reilly).
I hope this helps-
--
Xavier Mas
| |
|
|
| Peter Scott 2006-09-04, 6:57 pm |
| At 10:58 AM 9/3/2006, you wrote:
>Hi Peter,
>
>Sorry to bother again. What if I have some lines
>containing nothing(blank lines) and how do I remove
>these lines before using the map function?
>
>I try "chomp $data" but it doesn't work.
my @data = map [ split ] => grep /\S/, split /\r?\n/, param('data');
Please post all questions to the list, not privately.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
http://www.perlmedic.com/
|
|
|
|
|