Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Erm...why doesn't this work? 1

Status
Not open for further replies.

Siberdude

Programmer
Apr 4, 2000
29
GB
for($i = 2; $i <= $#files; $i++)
{

print &quot;<tr><td class=cells>$files[$i]</td>&quot;;

if(@already_banned =~ /$files[$i]/i)

{

print &quot;<td class=cells style=color:red>BANNED</td></tr>&quot;;

}

else

{

print &quot;<td class=cells><input type=checkbox value=$files[$i] name=addtoban></td></tr>&quot;;

}

}

idea of the script: checks out a list of names retrieved from a directory and put into the @files array. RUns a for loop on $#files and prints out stuff (html)
it checks the current $file[$i] against the contents of the @already_banned array (shouldn't this check all the elements of the array?) if it finds the $files[$i] in the @already_banned array it should prduce an out put with banned in red in a table cell, if not it should print out a check box.
but it doesn't do that... it acts as if none of the $files are found... i know two of them are in the array.
Can anyone tell me whats going wrong? and maybe why?
Thanks
Siberdude
siberdude@settlers.co.uk
 
hehe, thanks but that bits fine.. $#files gives the number of slots in the array and when $ is more than that i need it to stop producing tags.

the problem lies with the
if(@already_banned =~ /$files[$i]/i)

i think.. but i can't see whats wrong with it
Siberdude
siberdude@settlers.co.uk
 
I also am suspicious of this => &quot;if(@already_banned =~ /$files[$i]/i)&quot;
I've never seen a regex applied to an entire array like that. It may be perfectly legal/possible, but, I'm not familiar with it.

I have handled similar situations by storing the 'banned' list in a hash so it can be checked more easily/directly.

# build the hash with keys of banned_files and a value for each of '1'.
while ( &quot;getting banned files&quot;) { $already_banned{$file} = 1; }

# Then, when you want to see if a file is banned,
if ( $already_banned{$file}) # it either exists or it doesn't
{
print &quot;<td class=cells style=color:red>BANNED</td></tr>&quot;;
}
else
{
do other stuff...
}



HTH


keep the rudder amid ship and beware the odd typo
 
You're correct. the perl docs don't say what happens if the lvalue of a =~ operator is an array instead of a string, but since it's in a scalar context, it's probably taking the scalar value of @already_banned, which is the number of elements in the array. That's not what you want. you need to either loop thru the elements of the array, or turn the array into a string you can search with a join command. This code should work:
Code:
$already_banned = '|'.join('|',@aready_banned).'|';
And then:
Code:
if($already_banned =~ /\|$files[$i]\|/i;
It wraps each item with the bar character to prevent matching on partial items or across multiple items.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top