posting an array

M

Mark D Smith

Hi

i am posting a hidden values called accounts which is a number of usernames
pushed to an array.

print "<input type=hidden name=accounts value=\"@accounts\">";

the page that receives the data should be able to unpack the array but i am
having problems

&ReadParse();

foreach $line(@in) {
print "$line <br>\n";
}

shows accounts=username+username_001+username_002+username_003
as expected

but

$cnt=0;
@accounts = $in{'accounts'};
foreach $line(@accounts) {
print "$line<br>\n";
$cnt++;
}
print $cnt;

username username_001 username_002 username_003
1

all on 1 line, not 1 line for each value in the array.

what have i missed

Mark
 
J

James Taylor

$cnt=0;
@accounts = $in{'accounts'};
foreach $line(@accounts) {
print "$line<br>\n";
$cnt++;
}
print $cnt;

username username_001 username_002 username_003
1

all on 1 line, not 1 line for each value in the array.

what have i missed

It looks like $in{'accounts'} is a single line of text. It
gets assigned to @accounts which is then just a single item
array. Then the foreach loop only goes round once because
there is only one item in the @accounts array.

In order to process the space separated list of usernames
one at a time you must first convert the single line of text
to a list. The standard way of doing this would be to use
the 'split' function.

@accounts = split / /, $in{'accounts'};

Read up on the use of split here:

http://www.perldoc.com/perl5.6/pod/func/split.html

or by typing:

perldoc -f split
 
G

Gunnar Hjalmarsson

Mark said:
i am posting a hidden values called accounts which is a number of usernames
pushed to an array.

print "<input type=hidden name=accounts value=\"@accounts\">";

the page that receives the data should be able to unpack the array but i am
having problems

&ReadParse();

foreach $line(@in) {
print "$line <br>\n";
}

shows accounts=username+username_001+username_002+username_003
as expected

but

$cnt=0;
@accounts = $in{'accounts'};
foreach $line(@accounts) {
print "$line<br>\n";
$cnt++;
}
print $cnt;

username username_001 username_002 username_003
1

all on 1 line, not 1 line for each value in the array.

what have i missed

You seem to have missed a few things. First, what you post via a form
control is a string, not a list.

An array, such as

my @accounts = qw/name1 name2 name3/;

returns a string with the elements separated with spaces and
concatenated, if you surround it with double quotes.

If the usernames don't contain space characters, you can try

@accounts = split ' ', $in{'accounts'};

Another thing is that your Perl coding style seems to be outdated.
Please make it a habit to my() declare the variables and enable
strictures and warnings. Also, it may be a good idea to start using the
standard module CGI.pm instead of cgi-lib.pl (or whatever code you are
referring to with "&ReadParse();").
 
M

Mark D Smith

Gunnar Hjalmarsson said:
Mark D Smith wrote:
You seem to have missed a few things. First, what you post via a form
control is a string, not a list.

An array, such as

my @accounts = qw/name1 name2 name3/;

returns a string with the elements separated with spaces and
concatenated, if you surround it with double quotes.

If the usernames don't contain space characters, you can try

@accounts = split ' ', $in{'accounts'};

Another thing is that your Perl coding style seems to be outdated.
Please make it a habit to my() declare the variables and enable
strictures and warnings. Also, it may be a good idea to start using the
standard module CGI.pm instead of cgi-lib.pl (or whatever code you are
referring to with "&ReadParse();").

Hi

i do a my @accounts=();
it was only a code snippet.
as to cgi-lib.pl i know but i am updating some old code and it would take a
while to fix all the other bits.

@accounts = split ' ', $in{'accounts'}; works on second page and give the
results i wanted.

what i don't get is the in the first script i am building the array with

push (@accounts, $Euser);
in a while loop so i thought this would build an array which i could then
pass.

the same loop as the second script is used to display what is going to be
passed and that works.

Mark
 
J

James Taylor

what i don't get is the in the first script i am building
the array with

push (@accounts, $Euser);

in a while loop so i thought this would build an array
which i could then pass.

You need to show us the while loop if you want someone to tell
you what's wrong with it.
 
G

Gunnar Hjalmarsson

Mark said:
what i don't get is the in the first script i am building the array with

push (@accounts, $Euser);
in a while loop so i thought this would build an array

How you build the array is irrelevant.
which i could then pass.

You don't post a variable via a form control. *You post a string.*

I suggest that you read up on the basics about CGI to straight out a few
misconceptions.
http://www.cgi.resourceindex.com/Documentation/CGI_Tutorials/
 
J

Joe Smith

Mark said:
print "<input type=hidden name=accounts value=\"@accounts\">";

the page that receives the data should be able to unpack the array but i am
having problems

&ReadParse();
what have i missed

One of the things to consider is that you should 'use CGI;' instead
of cgi-lib.pl. It also appears that you are not aware of qq():

print qq(<input type=hidden name=accounts value="@accounts">\n);

or

print qq(<input type="hidden" name="accounts" value="$_">\n)
for @accounts;

but

$cnt=0;
@accounts = $in{'accounts'};
foreach $line(@accounts) {
print "$line<br>\n";
$cnt++;
}
print $cnt;

username username_001 username_002 username_003
1

all on 1 line, not 1 line for each value in the array.

Of course. print("@accounts") outputs a single string consisting
of the printable equivalent of all the items in the array
separated by blanks (or whatever $" is set to).

Either
1) leave the form creation script as is and change the receiving
script to convert the incoming string back into an array,
or
2) leave the form receiving script as is and change the creation
script to output multiple input tags with the same name.

Use
print qq(<input type="hidden" name="accounts" value="$_">\n)
for @accounts;
to create multiple lines of output like this:
<input type="hidden" name="accounts" value="$accounts[0]">
<input type="hidden" name="accounts" value="$accounts[1]">
<input type="hidden" name="accounts" value="$accounts[2]">
....

-Joe
 
B

Bart Lateur

Mark said:
i am posting a hidden values called accounts which is a number of usernames
pushed to an array.

print "<input type=hidden name=accounts value=\"@accounts\">";

the page that receives the data should be able to unpack the array but i am
having problems

Use one such line per array item. And please *html-escape* each item.

foreach (@accounts) {
printf "<input type=hidden name=accounts value=\"%s\">\n",
escapeHTML($_);
}
&ReadParse();

Ugh. Please don't tell me you're still using cgilib.pl.

escapeHTML() is exported if you use CGI like this:

use CGI ':standard';
 
M

Mark D Smith

Bart Lateur said:
Use one such line per array item. And please *html-escape* each item.

foreach (@accounts) {
printf "<input type=hidden name=accounts value=\"%s\">\n",
escapeHTML($_);
}


Ugh. Please don't tell me you're still using cgilib.pl.

escapeHTML() is exported if you use CGI like this:

use CGI ':standard';
Hi All

I am modifying a script supplied by someone else who uses cgilib.pl, i do
use CGI for scripts i write from the ground up.

with the input from this group i have resolved my problem.

thanks to all

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top