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: Intermediate:
Resetting an array?

 



Warren Bell
Deleted

Feb 12, 2000, 4:44 PM

Post #1 of 3 (870 views)
Resetting an array? Can't Post

I have some code that searches an array for a certain line and assigns a value to it. The it has to search the same array for the same line and assign a differeent valuse to it. The problem is after it has one valuse set, it can't assign a new value without looking in the array again. Is there any way to reset the array so that same line can be re-assigned? I have the part commented in the code below:

@message = $pop->HeadAndBody($i);

foreach $line (@message) {
if ($line =~ /^X-UIDL:\s(.*)$/i) {
$line = "X-Mozilla-Status: 8009\nX-Mozilla-Status2: 00000000\nX-UIDL: $1";
}
}

open(INB,">>$inbox") | | die "Can't open $inbox error $!";
print INB "$date\n";
map { print INB "$_\n" } @message; close(INB);

@message = $pop->HeadAndBody($i); # hack to refresh the X-Mozilla-Status

foreach $line (@message) {
if ($line =~ /^X-UIDL:\s(.*)$/i) {
$line = "X-Mozilla-Status: 0000\nX-Mozilla-Status2: 00000000\nX-UIDL: $1";
}
}

open(TRA,">>$trash") | | die "Can't open $trash error $!";
print TRA "$date\n";
map { print TRA "$_\n" } @message;
close(TRA);


This is part of my mail filter for windows if anyone's interested. It's working pretty solid now.
http://www2.jps.net/~wjbell/scripts/mailfilter.html


Jasmine
Administrator

Feb 12, 2000, 8:20 PM

Post #2 of 3 (870 views)
Re: Resetting an array? [In reply to] Can't Post

If while iterating through your array you maintain a count, you have the exact array position. This way, you can change the specific array element you want. Consider:

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


my $count = 0;
foreach $line (@message) {
if ($line =~ /^X-UIDL:\s(.*)$/i) {
$message[$count] = "X-Mozilla-Status: 0000\nX-Mozilla-Status2: 00000000\nX-UIDL: $1";
}
$count++;
}
</pre><HR></BLOCKQUOTE>


japhy
Enthusiast

Feb 13, 2000, 8:28 AM

Post #3 of 3 (870 views)
Re: Resetting an array? [In reply to] Can't Post

The idiom for changing an element in an array is"

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


for (@array) {
if (CONDITION) {
# modify $_
}
}
</pre><HR></BLOCKQUOTE>

You don't need to use a counter variable, or refer to $array[..].

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


for (@array) {
if (length > 50) { # if the element is more than 50 characters
substr($_, 47, 3, "..."); # make the last three characters "..."
}
}
</pre><HR></BLOCKQUOTE>

 
 


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

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