lundi 27 avril 2020

UPDATE column if column is greater than a value

I have a table with these columns

  • proxy_id (primary key)
  • proxy (IP of a proxy)
  • current_requests (number of current requests the proxy has handled since its last_cool_down)
  • total_requests (number of requests this proxy has fulfilled in its lifetime)
  • last_used (datetime of last time this proxy was used)
  • last_cool_down (datetime of last time this proxy was on cooldown)

After I have used a proxy I run this command (obviously with the current date and time, but we will assume the current datetime is 2020-04-28 13:10:03)

UPDATE proxy_table
SET
current_requests = current_requests + 1,
total_requests = total_requests + 1,
last_used = '2020-04-28 13:10:03',
last_cool_down = '2020-04-28 13:10:03'
WHERE proxy_id = 1

My issue is I must ALWAYS update current_requests and last_used no matter what. However, in the above code, it doesn't consider this condition. If current_requests + 1 == 20, then current_requests = 0 and last_cool_down = '2020-04-28 13:10:03'. How can I do this?

So essentially something like this

UPDATE proxy_table

if (current_requests + 1 == 20) {
    SET current_requests = 0;
    SET last_cool_down = '2020-04-28 13:10:03';
else {
    SET current_requests = current_requests + 1;
}

SET total_requests = total_requests + 1;
SET last_used = '2020-04-28 13:10:03';
WHERE proxy_id = 1;

I want to do this in one sql statement, rather than running 2 statements. My attempt at this didn't work:

UPDATE proxy_table
SET
    CASE
        WHEN current_requests + 1 = 20 THEN current_requests = 0, last_cool_down = '2020-04-28 13:10:03' ELSE current_requests = current_requests + 1
    total_requests = total_requests + 1,
    last_used = '2020-04-28 13:10:03'
WHERE
proxy_id = 1

When I receive an average of 60 or 70 the following if statement outputs 'D' and 'F' incorrectly

This is a program to randomly generate values and let the user practice multipliv]cation with those values. If incorrect its stored as array 0 and if correct its stored as array 1. The program then gives you an average and its corresponding letter grade... the letter grade doesn't work for the first 2 if statement however.

In the letterGrade Function the first two if statements are off at the endpoints 60 and 70 ex: 60 will output 'F' and 70 will output 'C'

        #include <iostream>
        #include<time.h>
        #include<cstdlib>
        #include<stdlib.h>
        #include<ctime>
        using namespace std;
//global variables
        int num;
        int numValues=0;
        int a,b,c,d;
        int ans;
        int answer;
        double total=0;
        double avg=0;
        int choice;
//functions
        void menu();
        void hard(int math[], int num);
        void easy(int math[], int num);
        void print(int math[], int num);
        double average(int math[], int num);
        double Average2(int math[], int num);
        void letterGrade(int math[], int num);
        int main()
        {
        srand(time(NULL));//random generator
        int math[numValues];
        cout << "Math Program" << endl;
        cout<<"practice multiplying one or two digit numbers"<<endl;
//menu and user choice
        menu();
        cin>>choice;
        while(choice>0 && choice<3)
        {
        switch (choice)
        {
        case 1://easy math 
            {
                total=0;
                 cout<<"enter the amount of practice trials you wish to complete"<<endl;
                cin>>numValues;
                easy(math,numValues);
                print(math,numValues);
                average(math,numValues);
                avg=average(math,numValues);
                cout<<endl;
                cout<<"Average: "<<avg*100;
                cout<<" ";
                letterGrade(math,numValues);
                break;

            }
            case 2://hard math
            {
                 total=0;
                 cout<<"enter the amount of practice trials you wish to complete"<<endl;
                cin>>numValues;
                hard(math,numValues);
                print(math,numValues);
                average(math,numValues);
                avg=average(math,numValues);
                cout<<endl;
                cout<<"Average: "<<avg*100;
                cout<<" ";
                letterGrade(math,numValues);
                cout<<endl;
                break;
              }
             }
           menu();
           cin>>choice;
             }

            if(choice==3)
           {
           cout<<"goodbye"<<endl;
           }
           return 0;
           }



           void easy(int math[], int num)//generates random A and Random B then user enters answer and it compares it with ab, array then is 0 if incorrect and 1 if correct
          {
          for(int i=0; i<num; i++)
          {
          a=rand()%10+1;
          b=rand()%10+1;
          cout<<"multiply "<<a<<"*"<<b<<""<<endl;
         answer=a*b;
         cin>>ans;
         if(ans==answer)
         {
            math[i]=1;
         }
        else
        {
            math[i]=0;
        }
        }

        }
        void hard(int math[], int num)//similar to above with numbers between 10 and 20 now
        {
        for(int i=0; i<num; i++)
        {
        c=rand()%11+10;
        d=rand()%11+10;
        cout<<"multiply "<<c<<"*"<<d<<""<<endl;
        answer=c*d;
        cin>>ans;
        if(ans==answer)
        {
            math[i]=1;
        }
        else
        {
            math[i]=0;
        }
        }
        }
        void print(int math[], int num)// prints your score as a 1 or 0
        {
       for(int i=0; i<num; i++)
        {
        cout<<math[i]<<" | ";
        total+=math[i];
       }
       }
       double average(int math[], int num)//avergaes your score
       {
       avg=total/numValues;
       return avg;
       }
       void letterGrade(int math[], int num)//displays your score as a letter grade
       {
      int letter;
      letter=avg*100;
      if(letter>=0 && letter<60)
      {
        cout<<"F"<<endl;
      }
      if(letter>=60 && letter<70)
      {
        cout<<"D"<<endl;
      }
      if(letter>=70 && letter<80)
      {
        cout<<"C"<<endl;
      }
      if(letter>=80 && letter<90)
      {
        cout<<"B"<<endl;
      }
      if(letter>=90 && letter<=100)
      {
        cout<<"A"<<endl;
      }
      }


      void menu()
     {
      cout<<"1. Easy math"<<endl;
       cout<<"2. Hard math"<<endl;
       cout<<"3. Exit"<<endl;
       }

Alternative for giant nested ifelse statement

I have a data frame that looks like the following:

Period No.  Frequency
1           Month
2           Month
3           Month
3           Quarter
6           Quarter     
9           Quarter
1           YTD
2           YTD
3           YTD

I want to add on a column called "Period" whose values are determined by what is in the Period No. AND Frequency columns. So:

Period No.  Frequency  Period
1           Month      1
2           Month      2
3           Month      3
3           Quarter    Q1
6           Quarter    Q2 
9           Quarter    Q3
1           YTD        YTD-Jan
2           YTD        YTD-Feb
3           YTD        YTD-Mar

Right now, I'm using nested if statements to do this. For example:

data$Period <-
  ifelse(
    (data$`Period No.` == '3') & (data$Frequency == 'Q1'), 'Q1',
    ifelse(
      (data$`Period No.` == '6') & (data$Frequency == 'Q2'), 'Q2',
      ifelse(
        (data$`Period No.` == '9') & (data$Frequency == 'Q3'), 'Q3', 'ERROR'
)
)
)

If I were to do this for every month for each iteration of Frequency, I would have 30 nested ifelse statements. I'm wondering if there's a more concise method to do what I'm trying to achieve?

Fill in empty values in column of dataframe by condition

I have the followin dataframe. Now I want to fill in the empty values in "product" by determining the value of the code 44 and 90. 44 should be "shirt" and 90 "sweater".

What's the best way to do this? With a for loop?

data = data.frame("code" = c(44,78,21,90,100,44,90), "product" = c("","hat","shoe","","umbrella","",""))
> data
  code  product
1   44         
2   78      hat
3   21     shoe
4   90         
5  100 umbrella
6   44         
7   90      

If / While Loop / Case Statement will not work @FullWeek in bold is throughing an error saying expecting ( or Select

If (Hoursweek > @FullWeek ) BEGIN WHILE PunchDate < '@Date BEGIN IF (@FullWeek - (Hoursweek +@Total)) < @FullWeek BEGIN (@FullWeek - (Hoursweek +@Total)) = @Total END IF (@FullWeek - (Hoursweek +@Total)) > @FullWeek BEGIN ((@FullWeek + @TotalHours) - (Hoursweek +@Total)) = @Total

                       CASE 
                            WHEN PunchDate = @StartDate THEN @TotalHours +Day1
                            WHEN PunchDate = @StartDate THEN @TotalHours +Day2
                            WHEN PunchDate = @StartDate THEN @TotalHours +Day3
                       END
                  END
          END

END

Conditional Statement for Strings "eq" not working in Perl

I am trying to put a condition to check whether two strings are equal or not? By using eq I am checking whether the Scalar variable is having the string 'png' but it does not seem to be working as it should be. When I am printing the scalar on the terminal it shows that it contains 'png'. The default IF statement is: if($fileEnding eq "png") then I manipulated by using single quotes enclosing the png: if($fileEnding eq 'png') but it didn't work that way either. I also however, did something like my $png = "png" initializing a scalar with 'png' and then checking the scalar by putting it in IF statement if($fileEnding eq $png). Nothing Happened!

Below is my code:

$fileEnding = `basename -s $ending`;                

            if($fileName !~ /(.*)?\.(.*)/) #This is just for checking if a file has an extension.
            {
                print $fileEnding;
                if($fileEnding eq "png")
                {
                    print "Yes it's a Png File\n";
                }

                if($fileEnding eq "gif")
                {
                    print "Yes it's a Gif File\n";
                }
            }

When I use ne instead of eq, the code executes, which is extremely weird and repetitive. Please see the attached image: enter image description here

The $fileEnding scalar does contain both 'png' and 'gif' which you can see on terminal. enter image description here

I am not able to figure out where I'm going off the rails.? Some help will be highly appreciated.

How can I use conditions in radio buttons (forms)?

I am struggling to find a way how to display information based on two options in a forms. What I mean is, there are two options in "Select Gender". If they choose "Male", then a paragraph should appear. If they choose "Female", then another paragraph should appear on the website. How can I do this?

This is the forms code:

    <form>
            Select Gender
            <br />
            <input type="radio" name="gender" id="male">
            <label for="male">Male</label>
            <br />
            <input type="radio" name="gender" id="female">
            <label for="female">Female</label>
            <br />
            <br />
    </form>