posting an array

Discussion in 'Perl Misc' started by Mark D Smith, Jul 23, 2005.

  1. Mark D Smith

    Mark D Smith Guest

    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
    Mark D Smith, Jul 23, 2005
    #1
    1. Advertising

  2. Mark D Smith

    James Taylor Guest

    In article <42e28daa$0$24867$>,
    Mark D Smith <> 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.
    James Taylor, Jul 23, 2005
    #2
    1. Advertising

  3. 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+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();").

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 23, 2005
    #3
  4. Mark D Smith

    Mark D Smith Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > 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
    Mark D Smith, Jul 24, 2005
    #4
  5. Mark D Smith

    James Taylor Guest

    In article <42e334f7$0$30396$>,
    Mark D Smith <> 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.
    James Taylor, Jul 24, 2005
    #5
  6. 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/Documentation/CGI_Tutorials/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 24, 2005
    #6
  7. Mark D Smith

    Joe Smith Guest

    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
    Joe Smith, Jul 24, 2005
    #7
  8. Mark D Smith

    Bart Lateur Guest

    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.
    Bart Lateur, Jul 25, 2005
    #8
  9. Mark D Smith

    Mark D Smith Guest

    "Bart Lateur" <> wrote in message
    news:...
    > 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
    Mark D Smith, Jul 25, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alanb
    Replies:
    2
    Views:
    516
    alanb
    Apr 23, 2004
  2. AmitKu
    Replies:
    7
    Views:
    476
    John Timney \(MVP\)
    Jan 8, 2007
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    470
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    196
    salsablr
    Dec 20, 2004
  5. phillyfan
    Replies:
    5
    Views:
    79
    William James
    Sep 11, 2005
Loading...

Share This Page