I was thinking I could do this on my own but I need some help.
I need to paste a list of email addresses from a local bands mail list into a textarea and process them my Perl script.
The emails are all in a single column; delimited by newlines:
email1@email.com
email2@email.com
email3@email.com
email4@email.com
email5@email.com
I would like to obviously get rid of any whitespace:
$emailgoodintheory =~ s/\s//ig;
and I am running them through basic validation:
if (Email::Valid->address($emailgoodintheory)) { #yada
I have tried all kinds of ways to get the list into an array.
my $toarray = CGI::param('toarray');
my @toarraya = split /\r?\n/, $toarray;
foreach my $address(@toarraya) {
print qq~ $address[$arrcnt]<br /> ~:
$arrcnt++;
}
Above is just to test to see if I was successful. I have no need to print them.
It just loops through, grabs the schedules .txt file and sends each member the band schedule. All that other stuff works but I cannot get the textarea into an array!
So, as you can see, I am pretty lost.
Thank you sir(s), may I have another quick lesson?
You seem a bit new to Perl, so I will give you a thorough explanation why your code is bad and how you can improve it:
1 Naming conventions:
I see that this seems to be symbolic code, but
$emailgoodintheoryis far less readable than$emailGoodInTheoryor$email_good_in_theory. Pick any scheme and stick to it, just don’t write all lowercase.I suppose that
$emailgoodintheoryholds a single email address. Then applying the regexs/\s//gor the transliterationtr/\s//will be enough; space characters are not case sensitive.Using a module to validate adresses is a very good idea. 🙂
2 Perl Data Types
Perl has three man types of variables:
Scalars can hold strings, numbers or references. They are denoted by the
$sigil.Arrays can hold an ordered sequence of Scalars. They are denoted by the
@sigil.Hashes can hold an unordered set of Scalars. Some people tend to know them as dicitonaries. All keys and all values must be Scalars. Hashes are denoted by the
%sigil.A word on context: When getting a value/element from a hash/array, you have to change the sigil to the data type you want. Usually, we only recover one value (which always is a scalar), so you write
$array[$i]or$hash{$key}. This does not follow any references sowill not print
123, butARRAY(0xABCDEF)and give you a warning.3 Loops in Perl:
Your loop syntax is very weird! You can use C-style loops:
where
@arraygives the length of the array, because we have a scalar context. You could also give$ithe range of all possible indices in your array:where
..is the range operator (in list context) and$#arraygives the highest available index of our array. We can also use a foreach-loop:Note that in Perl, the keywords
forandforeachare interchangeable.4 What your loop does:
Here you put each element of
@toarrayainto the scalar$address. Then you try to use it as an array (wrong!) and get the index$arrcntout of it. This does not work; I hope your programdied.You can use every loop type given above (you don’t need to count manually), but the standard
foreachloop will suit you best:A note on quoting syntax: while
qq~ quoted ~is absolutely legal, this is the most obfuscated code I have seen today. The standard quote"would suffice, and when usingqq, try to use some sort of parenthesis (({[<|) as delimiter.5 complete code:
I assume you wanted to write this:
And never forget to
use strict; use warnings;and possiblyuse utf8.