Cant get elements from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virtualweb
    New Member
    • Aug 2007
    • 30

    Cant get elements from an array

    Hello guys:

    Im a perl beginner (self taught) and Im unable to get elements from an array.


    Here is my situation step by step:

    1) A list of emails is submitted, (one email per line), through the texarea of a form named "Email_List " to a script that will check email sintax, etc
    List follows: (some emails have wrong syntax on purpose).

    ben_wade@gmail. com
    benwade@yuma.co m
    wade.ben@yuma.c om
    ben1981@yahoo.c o.uk
    @yuma
    yuma
    ben_wade.com
    benwade1951@yum a.usa.com
    @
    ben wade
    ben.wade.1951@u sa.org
    be@wade_org
    ben@wade.org
    wade.ben@yuma.c om
    ben1981@yahoo.c o.uk
    wade@cmzad.ll
    ben_w@ned.dex
    wadeb@rediff.co m
    george@gmail.vo m

    2) The script that processes this input reads like this:
    Code:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    require "cgi-lib.pl";
    &ReadParse;
    
    $Email_List = $in{'Email_List'};
    $Email_List =~ s/,\s*$//; $Email_List =~ s/^\s+//; $Email_List =~ s/\s+$//;
    
    @emails = "$Email_List";
    
    $count = '0';
    foreach $submitted_email (sort @emails){
    $count++;
    print qq!	
    <b><font face="arial" size="-1">
    $count) $submitted_email</font><br>
    !;
    }
    3) The above foreach loop treats the entire list in $Email_List which is passed to @emails as a single string, what I needed was a neat sorted list of emails printed to the browser one email per line. But I got a single big chunk of emails instead.

    4) So I added a split function as follows:
    Code:
    @emails = split(//, $Email_List);
    5) This added a space between each character of every email, so I added a space in the split function like this:
    Code:
    @emails = split(/ /, $Email_List);
    5) This didnt make my foreach loop present one email per line neatly sorted either. It broke the big chunk of emails into two smaller chunks of emails. So I thought pehaps I needed to add a carriage return to the spilt function like this:
    Code:
    @emails = split(/n\/, $Email_List);
    6) I can tell this last attempt is flat wrong because it gives me an error.

    My question is: How can I obtain a sorted list of emails, one per line, from the scalar $Email_List

    Thanx beforehand
    VirtualWeb
    PS: perhaps using cgi-lib.pl is a little antiquated but for this one time only I want to use it
    Last edited by numberwhun; Nov 8 '08, 01:27 PM. Reason: Please use code tags!!!
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Unless you are using perl 4 or a version of perl 5 that is very old using cgi-lib is just wrong. But anyway, try this, assumes each email address is entered on a seperate line in the textarea form widget:

    Code:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    require "cgi-lib.pl";
    &ReadParse(*in);
    
    $Email_List = $in{'Email_List'};
    @emails = split(/\r?\n/,$Email_List);
    print "$_<br>\n" for @emails; <-- sanity check;

    If you get a list from printing @emails you can then proceed to checking the elements. If you don't get a list but one long string, you need to determine why that is.

    6) I can tell this last attempt is flat wrong because it gives me an error.
    That because a newline is '\n' and not 'n\'. Where you have '\/' in your regexp is escaping the last delimiter in the regexp and causing a syntax error.

    Comment

    • virtualweb
      New Member
      • Aug 2007
      • 30

      #3
      Thank you KevinADC

      Your advice worked perfectly well the first try


      VirtualWeb

      Comment

      Working...