By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Cant get elements from an array

P: 30
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.com
wade.ben@yuma.com
ben1981@yahoo.co.uk
@yuma
yuma
ben_wade.com
benwade1951@yuma.usa.com
@
ben wade
ben.wade.1951@usa.org
be@wade_org
ben@wade.org
wade.ben@yuma.com
ben1981@yahoo.co.uk
wade@cmzad.ll
ben_w@ned.dex
wadeb@rediff.com
george@gmail.vom

2) The script that processes this input reads like this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. print "Content-type: text/html\n\n";
  3. require "cgi-lib.pl";
  4. &ReadParse;
  5.  
  6. $Email_List = $in{'Email_List'};
  7. $Email_List =~ s/,\s*$//; $Email_List =~ s/^\s+//; $Email_List =~ s/\s+$//;
  8.  
  9. @emails = "$Email_List";
  10.  
  11. $count = '0';
  12. foreach $submitted_email (sort @emails){
  13. $count++;
  14. print qq!    
  15. <b><font face="arial" size="-1">
  16. $count) $submitted_email</font><br>
  17. !;
  18. }
  19.  
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:
Expand|Select|Wrap|Line Numbers
  1. @emails = split(//, $Email_List);
  2.  
5) This added a space between each character of every email, so I added a space in the split function like this:
Expand|Select|Wrap|Line Numbers
  1. @emails = split(/ /, $Email_List);
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. @emails = split(/n\/, $Email_List);
  2.  
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
Nov 8 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. print "Content-type: text/html\n\n";
  3. require "cgi-lib.pl";
  4. &ReadParse(*in);
  5.  
  6. $Email_List = $in{'Email_List'};
  7. @emails = split(/\r?\n/,$Email_List);
  8. 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.
Nov 8 '08 #2

P: 30
Thank you KevinADC

Your advice worked perfectly well the first try


VirtualWeb
Nov 8 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.