conditional-statements

How can I assign a boolean condition result to a scalar variable in perl?

柔情痞子 提交于 2019-12-19 09:27:51
问题 I am doing the following, but it is not working properly: my $enabled = $hash && $hash->{'key'} && $hash->{'key'}->{'enabled'} && $hash->{'key'}->{'active'}; Is this an acceptable way to assign a boolean value to a scalar variable? My code is misbehaving in strange ways, as it is, and I believe it is because of this assignment. I have validated that the individual values exist for all of these keys and are set to a value. P.S. Sorry for being a noob! I googled for ~10 minutes and couldn't

Optional binding of nil literal vs. variable that is nil in Swift

↘锁芯ラ 提交于 2019-12-19 04:38:10
问题 In Swift, why does var x: Int? = nil if let y: Int? = x { ... } behave differently from if let y: Int? = nil { ... } My understanding of why the first case succeeds suggests that the second should as well, so I must not really be understanding. The latter is not failing because of an invalid assignment, nor because of optional chaining; and otherwise it seems the same as the former. Why does the latter fail, and how is it different from the former. Exactly at what point, and for what reason,

Rails 4 / Bootstrap 3: how to display a different navigation bar on different pages?

二次信任 提交于 2019-12-19 04:09:23
问题 Here is the _header.html.erb of our Rails 4 app, made with Bootstrap 3 components: <header class="navbar navbar-fixed-top navbar-inverse"> <div class="container"> <nav> <div class="col-xs-4"> <%= link_to "APP NAME", root_path, id: "logo" %> </div> <div class="col-xs-4 text-center"> <ul class="nav navbar-nav navbar-center"> <li><%= link_to "Features", features_path %></li> <li><%= link_to "Pricing", pricing_path %></li> <li><%= link_to "Blog", '#' %></li> </ul> </div> <div class="col-xs-4">

Else statement in Angular

ⅰ亾dé卋堺 提交于 2019-12-18 18:50:50
问题 How does angular2 propose to render <div *ngFor="let todo of unfinishedTodos"> {{todo.title}} </div> in case if unfinishedTodos.length >0 and text "empty" in another cases. P.S. <div *ngIf="unfinishedTodos && unfinishedTodos.length > 0"> <div *ngFor="let todo of unfinishedTodos"> {{todo.title}} </div> </div> <div *ngIf="!unfinishedTodos || unfinishedTodos.length <= 0"> empty </div> looks ugly 回答1: Syntax compatible with Angular 4.0 and beyond <ng-template #elseTemplate> Content displayed if

How much does the order of case labels affect the efficiency of switch statements?

♀尐吖头ヾ 提交于 2019-12-18 14:56:40
问题 Consider: if (condition1) { // Code block 1 } else { // Code block 2 } If I know that condition1 will be true the majority of the time, then I should code the logic as written, instead of: if (!condition1) { // Code block 2 } else { // Code block 1 } since I will avoid the penalty of the jump to the second code block (note: I have limited knowledge of assembly language). Does this idea carry forward to switch statements and case labels? switch (myCaseValue) { case Case1: // Code block 1 break

Python3 decorating conditionally?

狂风中的少年 提交于 2019-12-18 13:36:46
问题 Is it possible to decorate a function based on a condition? a'la: if she.weight() == duck.weight(): @burn def witch(): pass I'm just wondering if logic could be used (when witch is called?) to figure out whether or not to decorate witch with @burn ? If not, is it possible to create a condition within the decorator to the same effect? ( witch being called undecorated.) 回答1: You can create a 'conditionally' decorator: >>> def conditionally(dec, cond): def resdec(f): if not cond: return f return

Rails ActiveRecord conditions

我只是一个虾纸丫 提交于 2019-12-18 12:35:16
问题 Is there a way to create a condition like this? @products = Product.find(:all, :limit => 5, :conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']}) I would like to list all products not including product 1. Thx. 回答1: Rails 3 Use squeel gem. Product.where( :products => { :locale => 'en', :id.not_in => '1' }, :tags => { :name => ['a','b']} ).limit(5) Rails 2 Use AR Extensions for this. It supports the following condition modifiers: * _lt => less than *

Multiple 'or' condition in Python [duplicate]

我只是一个虾纸丫 提交于 2019-12-18 12:18:45
问题 This question already has answers here : How to test multiple variables against a value? (24 answers) Closed 5 years ago . I have a little code issue and it works with IDLE and not with Eclipse, can I write this : if fields[9] != ('A' or 'D' or 'E' or 'N' or 'R'): instead of this : if fields[9] != 'A' and fields[9] != 'D' and fields[9] != 'E' and fields[9] != 'N' and fields[9] != 'R': Thank you. 回答1: Use not in and a sequence: if fields[9] not in ('A', 'D', 'E', 'N', 'R'): which tests against

MySQL JOIN with IF conditions

只谈情不闲聊 提交于 2019-12-18 11:46:24
问题 I want to get some results via query simillar to: SELECT * FROM users LEFT JOIN IF (users.type = '1', 'private','company') AS details ON users.id = details.user_id WHERE users.id = 1 Any ideas? 回答1: SELECT * FROM users LEFT JOIN private AS details ON users.id = details.user_id WHERE users.id = 1 AND users.type = 1 UNION SELECT * FROM users LEFT JOIN company AS details ON users.id = details.user_id WHERE users.id = 1 AND users.type != 1 I think that is what you are trying to do, isn't it? As

Advantages of using condition variables over mutex

拈花ヽ惹草 提交于 2019-12-18 10:00:11
问题 I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads. What I found is : "Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling." (https://computing.llnl.gov/tutorials