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:
Comparing values from two arrays

 



wjbell
stranger

Dec 1, 2003, 10:44 AM

Post #1 of 4 (1045 views)
Comparing values from two arrays Can't Post

I have a messageboard script I'm working on. What I need to do is open the message index and get that into an array. Then open a posts read file for the user and get that into an array. Then, for each line in the index array, compare that to each line in the posts read array.

It's kind of hard to explain but here's my code commented so you can see what I'm trying to do. But it's not working and I'm not sure why.


Code
foreach $read (@reads) {  # each line has the format of "digit``digit``xx" 
$snum = 0;
$cnum = 0;
# get the digit values for each line to compare
if ($read =~ /(\d+)``(\d+)``xx/) {
$snum = $1;
$cnum = $2;
# compare each single line in @reads to every line in @news
foreach $line (@news) {
# get the comment number from the $line if $snum matches the number we got above
if ($line =~ /<\!--snum: $snum-->.* (\d+) comments.*/) {
$scnum = $1;
# if one number is bigger than the other, that means the post has new comments
# so we should add an image to the spesific $line in @news and then start the proccess all over again
# moving to the next entry in @reads
if ($cnum < $scnum) {
$line =~ s/(<\!--snum\: \d+--><b><div class=indextitle>)(.*)/$1<img src=\/images\/topic.gif>&nbsp\;$2/;
}
}
}
}
}


# once each line in reads is done, all the $line in @news should be substituted and then we can print out the modified @news array.



KevinR
Veteran


Dec 1, 2003, 11:05 AM

Post #2 of 4 (1042 views)
Re: [wjbell] Comparing values from two arrays [In reply to] Can't Post

print your variables so you can whats going on to try and figure out where the problem is:



Code
foreach $read (@reads) {  # each line has the format of "digit``digit``xx"  
$snum = 0;
$cnum = 0;
# get the digit values for each line to compare
if ($read =~ /(\d+)``(\d+)``xx/) {
$snum = $1;
$cnum = $2;
print "\$snum = $snum - \$cnum = $cnum<br>\n";
# compare each single line in @reads to every line in @news
foreach $line (@news) {
# get the comment number from the $line if $snum matches the number we got above
if ($line =~ /<\!--snum: $snum-->.* (\d+) comments.*/) {
$scnum = $1;
print "\$cnum = $cnum - \$scnum = $scnum<br>\n";
# if one number is bigger than the other, that means the post has new comments
# so we should add an image to the spesific $line in @news and then start the proccess all over again
# moving to the next entry in @reads
if ($cnum < $scnum) {
$line =~ s/(<\!--snum\: \d+--><b><div class=indextitle>)(.*)/$1<img src=\/images\/topic.gif>&nbsp\;$2/;
}
}
}
}
}

-------------------------------------------------


localfilmmaker
stranger

Feb 10, 2004, 1:15 PM

Post #3 of 4 (1024 views)
Re: [wjbell] Comparing values from two arrays [In reply to] Can't Post

You may need to escape the & sign in your regexp, like this:
[code]
$line =~ s/(<\!--snum\: \d+--><b><div class=indextitle>)(.*)/$1<img src=\/images\/topic.gif>\&nbsp\;$2/;
[/code]
-localfilmmaker
Spencer Christensen


davorg
Thaumaturge / Moderator

Feb 11, 2004, 1:41 AM

Post #4 of 4 (1021 views)
Re: [localfilmmaker] Comparing values from two arrays [In reply to] Can't Post


In Reply To
You may need to escape the & sign in your regexp


Why do you say that? & is never a special character in regular expressions so you never have to escape it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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