CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
spiting an array

 



darian
Deleted

Feb 17, 2000, 3:52 AM

Post #1 of 4 (467 views)
spiting an array Can't Post

@to = "me@my.com,you@you.com,we@we.com";

Ok the above is what will be sent via a form. Only I wont know how many will be sent each time. I need to be able to run code for each one of them. I know this is a really simple problem and I should probably just walk away and get some rest but I want to finish this darn thing up. so can anyone help me on this? Thanks


japhy
Enthusiast

Feb 17, 2000, 4:12 AM

Post #2 of 4 (467 views)
Re: spiting an array [In reply to] Can't Post

Ok, if you mean the ARRAY @to has one element, the value of which is:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


this@there.com, mine@yours.org, what@ever.net
</pre><HR></BLOCKQUOTE>

And you want to REPOPULATE the array by splitting that first element into many elements, then I'd suggest you do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@to = split /,\s*/, $to[0];
# or, if there might be more than one element
@to = split /,\s*/, join ", ", @to;
</pre><HR></BLOCKQUOTE>

Then, @to in scalar context will tell you how many email addresses there are.


japhy
Enthusiast

Feb 18, 2000, 5:38 AM

Post #3 of 4 (467 views)
Re: spiting an array [In reply to] Can't Post

When I said "@to in scalar context", I meant

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$number = @email;
</pre><HR></BLOCKQUOTE>

That will give $number some numeric value, saying how many elements there are in @email.

If you want to iterate over the array, use a for (or foreach) loop:


Code
foreach $addr (@email) { 
# do something with $addr
}

for $addr (@email) {
# do something with $addr
}

foreach (@email) {
# do something with $_
}

for (@email) {
# do something with $_
}
[/code

[This message has been edited by japhy (edited 02-18-2000).]


darian
Deleted

Feb 18, 2000, 10:35 AM

Post #4 of 4 (467 views)
Re: spiting an array [In reply to] Can't Post

Ok I understand how to get it in scalar context but what I cant seem to work out is how to know which I am using. What I want to say is:

foreach record in the array @to then we do something.

However, how do I get each record out of the array without knowing what their scalar context is. I just need it to go through each one and do something so could I do a foreach statement on the repopulated @to array?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives