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

unless eq versus if ne 1

Status
Not open for further replies.

MrCBofBCinTX

Technical User
Dec 24, 2003
164
US
I am having a problem with the following puzzle.
The first version works fine, the second fails.
Is there a way to successfully make the if version work?


Perl:
unless ( $command eq "ViewRecords"
    || "InsertRecordGroupForm"
    || "UpdateRecordForm"
    || "InsertRecordGroup"
    || "UpdateRecord"
    || "ShowColumns"
    || "ShowTables" )
{
etc
}

but this does not
Perl:
if ( $command ne "ViewRecords"
    || "InsertRecordGroupForm"
    || "UpdateRecordForm"
    || "InsertRecordGroup"
    || "UpdateRecord"
    || "ShowColumns"
    || "ShowTables" )
{
etc
}


 
Hi

None of your conditions work as you probably expect. I would rewrite them as
Perl:
[navy]@list[/navy][teal]=[/teal][b]qw[/b][teal]{[/teal]ViewRecords InsertRecordGroupForm UpdateRecordForm InsertRecordGroup UpdateRecord ShowColumns ShowTables[teal]}[/teal][teal];[/teal]

[gray]# with grep[/gray]
[b]if[/b] [teal](![/teal] [b]grep[/b] [teal]{[/teal][navy]$_[/navy] [b]eq[/b] [navy]$command[/navy][teal]}[/teal] [navy]@list[/navy][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [green][i]"etc\n"[/i][/green][teal];[/teal]
[teal]}[/teal]

[gray]# with smartmatch operator[/gray]
[b]if[/b] [teal](! ([/teal][navy]$command[/navy][teal]~~[/teal][navy]@list[/navy][teal]))[/teal] [teal]{[/teal]
  [b]print[/b] [green][i]"etc\n"[/i][/green][teal];[/teal]
[teal]}[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Thanks, that looks good.
I picked up the book Perl Best Practices, which has inspired me to rewrite a lot of the junk that was in my code.
There were a lot of inefficient SQL tests I had that I have now simplified without loops or extra variables.
This change is to make the test for valid commands and then run them.

I also have some subroutines that work, but are brutally difficult to re-use for table additions/changes. I am now going to rewrite those for clarity when using.

Very good book!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top