Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > posting an array

Reply
Thread Tools

posting an array

 
 
Mark D Smith
Guest
Posts: n/a
 
      07-23-2005
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+userna me_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

 
Reply With Quote
 
 
 
 
James Taylor
Guest
Posts: n/a
 
      07-23-2005
In article <42e28daa$0$24867$(E-Mail Removed)>,
Mark D Smith <(E-Mail Removed)> wrote:
>
> $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

--
James Taylor, London, UK PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-23-2005
Mark D Smith wrote:
> 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+userna me_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();").

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Mark D Smith
Guest
Posts: n/a
 
      07-24-2005

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mark D Smith wrote:

<snip>
>
> 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();").
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl


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








 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      07-24-2005
In article <42e334f7$0$30396$(E-Mail Removed)>,
Mark D Smith <(E-Mail Removed)> wrote:
>
> 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.

--
James Taylor, London, UK PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-24-2005
Mark D Smith wrote:
> Gunnar Hjalmarsson wrote:
>>
>> what you post via a form control is a string, not a list.

>
> 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/Doc...CGI_Tutorials/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-24-2005
Mark D Smith wrote:

> 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
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      07-25-2005
Mark D Smith wrote:

>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';

--
Bart.
 
Reply With Quote
 
Mark D Smith
Guest
Posts: n/a
 
      07-25-2005
"Bart Lateur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mark D Smith wrote:
>
> >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';
>
> --
> Bart.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URL Posting Fails in Medium Trust (3rd time posting this w/ zero replies so far) AmitKu ASP .Net 7 01-08-2007 07:31 PM
CROSS-POSTING, OR MULTI-POSTING, OR NEITHER? Colin D Digital Photography 56 03-08-2006 08:31 PM
[OT] : Top Posting vs Bottom Posting Wayne Wastier Windows 64bit 7 07-17-2005 03:57 PM
Top Posting vs. Bottom Posting scaredkitty Computer Support 37 04-06-2005 12:27 AM
Everytime I hover cursro over a posting, it crosses out with red mark on it.. on every posting alanb ASP .Net 2 04-23-2004 02:23 PM



Advertisments