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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditionals

Status
Not open for further replies.

DonP

IS-IT--Management
Joined
Jul 20, 2000
Messages
684
Location
US
I'm having a problem with conditionals. When the value is numeric, it works but as text characters it does not:

Neither of these work:

[tt]if ($_->[9] eq 'SP'){
print "Some text";
}[/tt]

[tt]if ($_->[9] eq "SP"){
print "Some text";
}[/tt]

but this does:

[tt]if ($_->[9] == 1000){
print "Some text";
}[/tt]

This code is within a similar conditional statement that is always numeric. Are there issues with trying to use a text character conditional within a numeric one? Am I doing something else wrong? (Of course, I changed the matching field value in the flatfile database to test each of these.)
Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Try this:

You have:
[tt]
if ($_->[9] eq "SP"){
print "Some text";
}
[/tt]

Change to

[tt]
if ($_[9] eq "SP"){
print "Some text";
}
[/tt]

See what happens when you do that.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Thanks Vic! I'll give it a try but the code it is written to be $_->[9] and that's the way the field values are everywhere else in the script. It does work as $_->[9] when the conditional value is numeric. Is there something that would require it to be different for a string value?
Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Well, the reason I say to take the -> out is that @_ is an array, and I have never seen the -> used in reference to an array.

If it were a hash, it would be different.

Can you post some more of the code?

Thanks.
-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
I wrote the code a long time ago but the sort subroutine was written by someone else. I doubt any of this has anything to do with my original question but I'll try to post some of it just in case. The whole thing is far to big to post here, being several pages long, so hopefully there excerpted snippets will make sense:

[tt]open(DBITEMS, "$database");

while (<DBITEMS>){
push @database, [split (/\s*\|\s*/, $_) ];
}
my @sorted =
sort {&sort_fields} @database;

# Sort on item name, then sub-sort on item type
sub sort_fields {
if ($a->[1] cmp $b->[1]) {
return $a->[1] cmp $b->[1]}
else {
return $a->[4] cmp $b->[4]}
}[/tt]

some HTML

[tt]saved;
foreach (@sorted)
{
if ($saved->[1] eq $_->[1])
{
next;
}
$type = &quot;$_->[1]&quot;;
$type =~ tr/ /+/;
print &quot;<li><a href=\&quot;form.cgi?$type\&quot;><b>$_->[1]$s</b></a>\n&quot;;
$saved = $_;
}[/tt]

More HTML

[tt]foreach (@sorted){
if ($_->[1] =~ /^$query$/i){
[/tt]

Still more HTML and here is where the conditionals causing the problem are

[tt]}
close (DBITEMS);[/tt]

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
The -> notation is only used when you've got an array REFERECNE. If $_ contains an array reference, then $_->[9] refers to the 10th element of the array referenced by $_.

The reason the code Don posted above works is because he's causing the results of the split to be turned into an anonymous array (by the [] around the split), and pushing a reference to that anonymous array into @database. Then when he's sorting, what's getting passed to the sort routine are refernces to anonymous arrays, so $a->[1] refers to the 2nd element of the anonymous array whose reference was passed as $a, and similarly for $b (all sort routines get $a and $b assigned automatically by the sort statement). Then he stores the sorted array of array references into @sorted. In the for loops, since he doesn't specify a variable for the for, the default is $_. Since it's still an array reference, $_->[1] refers to the second element of the array whose reference is in $_. This is a common way of handling arrays-of-arrays. See the perldsc (data structures cookbook) man page for more info. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Tracy, Great explination (sorry for the typos, which made my text a bit hard to read)! But do you have any idea why a character conditional will not work but a numeric one will for that field? I am using a character conditional on other values, which seem to work just fine and am puzzled. Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
I don't have any idea why the might be a problem, since perl will casually switch between numeric and string interpretations of a variable without any problems. Have you tried printing the value of the field you're trying the compare on to see if it actually contains what you think it does?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes, and it does. It makes no sense at all. I ended up patching it by making the field have a numeric value as it does in other entries of that same field and it worked just fine, but I would really like to fix it properly. Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
I don't understand it, it shouldn't make any difference. I'll run a few tests and see if I can come up with something.
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.

Similar threads

Replies
19
Views
384
Replies
5
Views
102

Part and Inventory Search

Sponsor

Back
Top