jeudi 26 novembre 2020

Why regex not work in bash in following situation? [duplicate]

BranchName=$1

RegEx="(refs\/heads\/)?([Rr]elease|[Dd]ev|hotfix)(-v[\d]+\.[\d]+\.[\d]+)(--)([\w][\w-]+)(--)[\d]+$"

echo "Branch name for test is: ${BranchName}"
if [[ ${BranchName} =~ ${RegEx} ]]; then
   echo "Branch name IN correct format: ${BranchName}"
   exit 0
else
   echo "Branch name not in correct format: ${BranchName}"
   exit 1
fi

string for testing: "refs/heads/release-v8.1.7--testBranchRename--323323"

REGEX correct according https://rubular.com/

How to do this IF with ternary operators

how can I do this IF with ternary operators?

if (img != null) {
  return img;
} else if (!user) {
  return 'img.svg';
} else if (user) {
  if (user.img && !camera) {
    return `imgServer`;
  } else if (!user.img && !camera) {
    return 'img.svg';
  } else if (user.img && camera) {
    return camera;
  } else if (!user.img && camera) {
    return camera;
  }
} 

I already tried it in several ways, but none of them work for me.

Excel VB to copy rows from 1 sheet to another based 2 criteria met

I have zero VBA experience. I am trying to use an IF statement to met two criteria in rows on a sheet labeled Master. the criteria are if Column D = "Active" & Column E = "Spanish", I want to copy that row over to a Sheet labeled Spanish, but I do not want Columns D & E to copy over to Spanish sheet.

Here is what I am working on, but I keep getting a Compiling error. What am I doing wrong?

Sub TransferValues()

Dim Emp1a As String, Emp1b As String
With Worksheet("Sheet1")
    If .Range("D2:D30") = "Active" And .Range("E2:E30") = "Spanish"
    Emp1a = .Range("B2:C30")
    Emp1b = .Range("F2:I30")
End With

 Sheet2.Range("B2:C30") = Emp1a
 Sheet2.Range ("D2:G30") - Emp1b
    
 Debug.Print Emp1a
 Debug.Print Emp1b

End Sub

How do I sum certain partially-duplicated rows but not all duplicated rows (based on a conditional statement)?

My current dataset looks something like this:

INVOICE_ID  MONTH  AMOUNT
1            2     -500
1            2     -500
1            2     1000
1            3     30
1            3     10
1            3     -30
2            5     40
2            5     60
3            4     50
....

I want to convert this to look like:

INVOICE_ID  MONTH  AMOUNT
1            2      0
1            3     10
2            5      40
2            5      60
3            4      50

So for rows that have an INVOICE_ID and MONTH that match a row with the same INVOICE_ID and MONTH and a negative AMOUNT, I want to consolidate these rows into one row that is the sum of the AMOUNTs in all matching rows. However, for rows that have an INVOICE_ID and MONTH that match rows with the same INVOICE_ID and MONTH but only positive AMOUNTs, I want to leave this data untouched.

I'm not a great R user, so this feels above my level and I'm not sure how to approach it. Any help would be greatly appreciated!

Can you add *If* statements in feedforward pass of NN - Pytorch

I am trying to test multiple different models of an MLP (dropouts or no dropouts, depth, size of layers) and was thinking instead of manually creating a different MLP for each run I would add if logics when defining my nn architecture, such as this snippet;

#Define Forwards Pass
def forward(self, x):
if Dropout = 0:
  if num_of_layers = 4:
    # flatten image input
    x = x.view(-1,28*28)
    x = self.fc1(x)
    x = F.relu(x)enter code here

Does pytorch let you do such a thing, because I got a Syntax Error for my first if statement?

Why is my Button failing to perform in my GUI?

My code below does not respond and open the plain message when I click the button after running it, I've checked the code a few times and can't see where I'm going wrong, can anyone see if there's something I missed as I think it should all be implemented correctly but the JButton is not opening my menu items. Many thanks

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final JFrame frame=new JFrame("CoinSorterGUI");
    
    class MyItemListener implements ItemListener{
        
        public void itemStateChanged(ItemEvent ev) {
            boolean selected = (ev.getStateChange()== ItemEvent.SELECTED);
            AbstractButton button =(AbstractButton) ev.getItemSelectable();
            String command = button.getActionCommand();
            if (selected) {
                int messageType = -1;
                String message = "";
                if (command.equals("CoinCalculator")) {
                    messageType = JOptionPane.PLAIN_MESSAGE;
                    message = "Welcome to the CoinCalculator, click OK to begin program";
            }else if (command.equals("Multiple CoinCalculator")) {
                    messageType = JOptionPane.PLAIN_MESSAGE;
                    message = "Welcome to the Multiple CoinCalculator, click OK to begin program";
            }else if (command.equals("Print Coin List")) {
                    messageType = JOptionPane.PLAIN_MESSAGE;
                    message = "Current coin denominations are; £2, £1, 50p, 20p and 10p";
            }else if (command.equals("Set details")) {
                messageType = JOptionPane.INFORMATION_MESSAGE;
                message = " ** Set details Sub Menu ** "
                        + "    1 -  Set Currency       "
                        + "    2 -  Set minimum input value"
                        + "    3 -  Set Maximum input value"
                        + "    4 -  Return to main menu";
            }else if (command.equals("Display program configurations")) {
                messageType = JOptionPane.PLAIN_MESSAGE;
                message = "The current currency is £, the minimum input value is 0 and the maximum input value is 10000";
            }
                JOptionPane.showMessageDialog(frame,
                        message,
                        "CoinSorter",
                        messageType);
            }
                    
          }
        }

        JButton r1 = new JButton("CoinCalculator");
        r1.setActionCommand("CoinCalculator");
 
        JButton r2 = new JButton("Multiple CoinCalculator");
        r2.setActionCommand("Multiple CoinCalculator");
 
        JButton r3 = new JButton("Print Coin List");
        r3.setActionCommand("Print Coin List");
 
        JButton r4 = new JButton("Set details");
        r4.setActionCommand("Set details");
        
        JButton r5 = new JButton("Display program configurations");
        r5.setActionCommand("Display program configurations");
    
        MyItemListener myItemListener = new MyItemListener();
        r1.addItemListener(myItemListener);
        r2.addItemListener(myItemListener);
        r3.addItemListener(myItemListener);
        r4.addItemListener(myItemListener);
        r5.addItemListener(myItemListener);
        
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);
        group.add(r3);
        group.add(r4);
        group.add(r5);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 500);
        Container cont = frame.getContentPane();
 
        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Welcome to the CoinSorter, please choose from the options: "));
        cont.add(r1);
        cont.add(r2);
        cont.add(r3);
        cont.add(r4);
        cont.add(r5);
 
        frame.setVisible(true);

}

}

What's the problem with fgets() input in c within a switch, loop or if statement?

I tried to ask for some string input with the fgets() function, it is working as long as I don't try to construct a loop or if statement around it. I thought I need to solve this with a pointer, but the char array named "city" already acts as one. What's my fault?

#include <stdio.h>
//#include <string.h>

int main(){
int anfangEnde;
char city[20];

/*This is a comment*/
do{
    printf("Tippe 1 fuer fortfahren und 2 fuer beenden:\n");
    scanf("%d", &anfangEnde);
    switch(anfangEnde){
    case 1:
        printf("gebe Stadtnamen ein: \n");
        fgets(city, 20, stdin);
        //Input geht nicht in if Statements. KP wieso.
        printf("%sund es hat hingehauen.\n", city);
        break; 
    case 2:
        printf("Programm beendet\n");
        break; 
    default: 
        printf("Fehlerhafte Eingabe\n");
    }
}while((anfangEnde != 1) && (anfangEnde != 2));
return 0;
}