Scenario: In my worksheet I have data in multiple columns on the following format (this is an ASCII generated table to facilitate visualization, the symbols are not on the original spreadsheet):
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| Count | Indentifier1 | Identifier2 | Identifier3 | ProductName | Location | Type | Status | RegistryDate |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 1 | azjzjzj3611a | | | Super electric board - high load | SEA | General | Sold out -- Used -- | 4/17/2019 4:19 |
| | | | | | | | Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 2 | y000zj9y30 | | | electric board - low battery | SEA | General | Sold out -- Used -- Good | 5/12/2015 3:48 |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 3 | | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 4 | a30y970y3 | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 5 | | | | Ball 10-Type1 | BEACH | TEST - Utility - Small-Item | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 6 | azjzjzja9zjy9 | | | Ball 10-Type1 | BEACH | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 7 | azjzjzja9zjy9 | | | Ball 10-Type1 | BEACH | TEST - Utility - Small-Item | Sold out -- Used - Good | 6/10/2013 0:00 |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 8 | a3044aa69 | 1007750 | | Ball 10-Type1 | | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 9 | | | | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 10 | a3044aa69 | | BLL101 | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 11 | a3044aa69 | 1007750 | BLL101 | BALL 10-TYPE1 | Beach | TEST - Utility | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 12 | y0003aa67 | 36021 | | Ball 5-Type1 | | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 13 | | | | Ball 5-Type1 | RIDGE | Group - Special | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 14 | y0003aa67 | | | Ball 5-Type1 | RIDGE | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 15 | y0003aa67 | | BLL051 | Ball 5-Type1 | RIDGE | Group - Special | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 16 | y0003aa67 | 36021 | BLL051 | BALL 5-TYPE1 | Ridge | Group - Special | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
Obs.: As per the table, the data can have multiple entries, where different information is displayed in each column. The differences may be either the format (upper/lower-case) or content (has data or not).
Objective: I am trying to retrieve the information from this table, without loosing data. Ex: If two rows have the same data, but one is lower case whereas the other is upper case, either could be retrieved (no preference), but if there is different data (maybe there is a slight difference in the item name or status), I am trying to retrieve all the data.
Done so far: I was able to simple create an array, loop it, and compare the data manually, but the result is sub-optimal, since it just aggregates the data in the same cell where it is different. Which would still require manual checking/cleaning.
Obs2.: This table has around 22 thousand rows.
Obs3.: Another issue of this data-set is that none of the columns is completely filled. Sometimes I have data for all identifiers and item name, sometimes an identifier is missing, sometimes the name is missing, and sometimes just one identifier is available (no name or anything else).
Question: Is there a way to do this?
Wanted output:
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| Count | Indentifier1 | Identifier2 | Identifier3 | ProductName | Location | Type | Status | RegistryDate |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 1 | azjzjzj3611a | | | Super electric | SEA | General | Sold out -- Used -- | 4/17/2019 4:19 |
| | | | | board - high load | | | Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 2 | y000zj9y30 | | | electric board - low battery | SEA | General | Sold out -- Used -- Good | 5/12/2015 3:48 |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 3 | a30y970y3 | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 4 | a3044aa69 | 1007750 | | Ball 10-Type2 | BEACH | TEST - Utility - Small-Item | Sold out -- Used - Good | 6/10/2013 0:00 |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 5 | a3044aa69 | 1007750 | BLL101 | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 6 | y0003aa67 | 36021 | BLL051 | BALL 5-TYPE1 | Ridge | Group - Special | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
Code for array approach:
Sub cleanup_detail()
Dim raw_array As Variant
Dim w As Workbook
Dim loopvar1 As Long
Set w = ThisWorkbook
raw_array = w.Worksheets("Sheet1").UsedRange
For loopvar1 = 2 To UBound(raw_array, 1)
If raw_array(loopvar1 + 1, 2) = "" Or raw_array(loopvar1 + 1, 2) = "0" Then
If raw_array(loopvar1 + 1, 3) = "" Or raw_array(loopvar1 + 1, 3) = "0" Then
If raw_array(loopvar1 + 1, 4) = "" Or raw_array(loopvar1 + 1, 4) = "0" Then
If raw_array(loopvar1 + 1, 5) = "" Or raw_array(loopvar1 + 1, 5) = "0" Then
Next loopvar1
ElseIf raw_array(loopvar1 + 1, 5) = raw_array(loopvar1, 5) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 4) = raw_array(loopvar1, 4) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 3) = raw_array(loopvar1, 3) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 2) = raw_array(loopvar1, 2) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
Next loopvar1
End Sub
I am trying to understand how Bitwise operators work in C, but I have an misunderstanding about the << operator.
I have the following code:
#include <stdio.h>
int Add(int x, int y);
int Add(int x, int y)
{
while ( y != 0 ) /// Iterate till there is no carry
{
int carry = x & y; /// carry now contains common set bits of x and y
x = x ^ y; /// Sum of bits of x and y where at least one of the bits is not set
y = carry << 1; /// Carry is shifted by one so that adding it to x gives the required sum
}
return x;
}
int main( void )
{
printf( "%d", Add( 13, 17 ) );
return 0;
}
If I understand correctly works like this:
First Iteration:
|=======================================|
| |
| while ( y != 0 ) |
| while ( 17 != 0 ) |
| while ( 10001 != 00000 ) |
| |
| c = x & y; |
| 1 = 13 & 17 |
| 00001 = 01101 & 10001 |
| |
| x = x ^ y |
| 28 = 13 ^ 17 |
| 11100 = 01101 ^ 10001 |
| |
| y = c << 1 |
| 17 = 1 << 1 |
| 10001 = 00001 << 00001 |
| 00010 = 00001 << 00001 |
| |
|=======================================|
Second Iteration:
|=======================================|
| |
| while ( y != 0 ) |
| while ( 2 != 0 ) |
| while ( 00010 != 00000 ) |
| |
| c = x & y; |
| 0 = 28 & 2 |
| 00000 = 11100 & 00010 |
| |
| x = x ^ y |
| 30 = 28 ^ 2 |
| 11110 = 11100 ^ 00010 |
| |
| y = c << 1 |
| 2 = 0 << 1 |
| 00010 = 00000 << 00001 |
| 00000 = 00000 << 00001 |
| |
|=======================================|
Then Y becomes 0 and X returns 30.
Now in the following code I have an issue:
#include <stdio.h>
int main( void )
{
int x = 13;
int y = x << 1; /// 11010 = 01101 << 00001
x = 0 << 1; /// 00000 = 00000 << 00001
printf("y = %d\n", y ); /// 26 | 11010
printf("x = %d\n", x ); /// 26 | 11010
return 0;
}
Here if I understand right we shift all bits one to the left:
int y = x << 1; /// 11010 = 01101 << 00001
But what exactly happens here:
x = 0 << 1; /// 00000 = 00000 << 00001
Does x get cleared and filled with the rezult of 0 << 1 ?
Does x get cleared and filled with the rezult of 0 << 1 ?
x is just assigned the value of the expression 0 << 1. Zero left or right shifted by any amount remains 0.
So this means that the representation of the First and Second Iteration is correct?
It is correct, except that substitution of the old values of variables (on the lhs) is a bit confusing as in the following cases.
17 = 1 << 1
10001 = 00001 << 00001
and
2 = 0 << 1
00010 = 00000 << 00001
Instead depict it as:
y = 1 << 1
y = 00001 << 00001
and
y = 0 << 1
y = 00000 << 00001
n << k is actually n * (2^k) as long as you have enough bits available to keep all of the resulting bits. So 0 << k is 0 * (2^k) = 0 whatever the (positive integer) value of k is.
Note that for usual 32 bit integers, a number on p = 17 bits or more, like 65537 (0x0001_0001), will stop behaving like the multiplication once k is greater or equal to (32+1)-p = (32+1)-17 = 16, as for example 65537 << 16 is 0x1_0001_0000 which is using 33 bits and is truncated to 0x0001_0000 = 65536.
With 65536 << 15 you may also start having strange results as the result, 0x1000_0000, is changing the left most bit, which is the sign bit if you're not using unsigned values...
I'm looking to use VBA to transform a raw data extract into a flattened table for querying. Currently, I have a raw data table in Excel that summarizes the status of Phases A, B, and C for a given Engagement (note: some Engagements may not have data for all 3 phases).
Row| EngagementID | A_date | A_status | B_date | B_status | C_date | C_status
1 | 201 | 2/2 | Approved | | | |
2 | 201 | | | 3/5 | Approved | |
3 | 201 | | | | | 4/1 | Pending
4 | 203 | 2/12 | Submitted| | | |
5 | 203 | | | 2/20 | Approved | |
6 | 207 | 2/5 | Approved | | | |
I need to flatten the table to look like something this:
Row| EngagementID | Date | Status
1 | 201 | 2/2 | Approved
2 | 201 | 3/5 | Approved
3 | 201 | 4/1 | Pending
4 | 203 | 2/12| Submitted
5 | 203 | 2/20| Approved
6 | 207 | 2/5 | Approved
Additionally, I'd like to add a column for the Phase so that I can "tag" each row with the Phase (A, B, or C) that it is associated with.
I've tried the following VBA code, but it flattens the table vertically, as opposed to horizontally (merging 3 rows into 1, as opposed to 3 columns into 1):
Private Sub test()
Dim R As Long
Dim i As Integer
i = 1
R = 2
Count = 0
Do While Not IsEmpty(Range("A" & R))
If Cells(R, 1).Value = Cells(R + 1, 1).Value Then
Count = Count + 1
Else
i = 1
Do While i <= Count
Cells(R - Count, 2 + (2 * i)).Value = Cells(R - Count + i, 2 + (2 * i))
Cells(R - Count, 3 + (2 * i)).Value = Cells(R - Count + i, 3 + (2 * i))
i = i + 1
Loop
i = 1
Do While i <= Count
Rows(R - Count + i).Delete
i = i + 1
R = R - 1
Loop
Count = 0
End If
R = R + 1
Loop
End Sub
Please help!!
Try this code
Sub Test()
Dim a As Variant
Dim b As Variant
Dim i As Long
Dim j As Long
Dim k As Long
a = Range("A1").CurrentRegion.Value
ReDim b(1 To UBound(a, 1) * 3, 1 To 3)
For i = 2 To UBound(a, 1)
For j = 2 To UBound(a, 2) Step 2
If a(i, j) <> "" And a(i, j + 1) <> "" Then
k = k + 1
b(k, 1) = a(i, 1)
b(k, 2) = a(i, j)
b(k, 3) = a(i, j + 1)
End If
Next j
Next i
Range("J1").Resize(k, UBound(b, 2)).Value = b
End Sub
Given a maple expression like
f := (y + 5)^2 + x^2
How can I replace all occurrences of the square function with a custom function? The result I want to have would be
square(y + 5) + square(x)
I tried something like
subs({x^2 = square(x)}, f)
However, that only replaces the x^2 expression. I could of course list explicitly the (y + 5) term as well, but I want to replace any occurrence of (.)^2 without listing everything explicitly.
How can I do this in Maple?
You can accomplish this using either subsindets or applyrule.
expression:=(y-5)^2+3+sin(((z-1/3)^2/(t-(y-s)^2)))+F(f(17+p^2)^2);
/ 2 \
| / 1\ |
| |z - -| | / 2\
2 | \ 3/ | | / 2 \ |
expression := (y - 5) + 3 + sin|------------| + F\f\p + 17/ /
| 2|
\t - (y - s) /
applyrule(_a::anything^2=square(_a), expression);
/ / 1\ \
| square|z - -| |
| \ 3/ |
square(y - 5) + 3 + sin|-----------------| + F(square(f(square(p) + 17)))
\t - square(y - s)/
subsindets(expression, '`^`'(anything,2), u->square(op(1,u)));
/ / 1\ \
| square|z - -| |
| \ 3/ |
square(y - 5) + 3 + sin|-----------------| + F(square(f(square(p) + 17)))
\t - square(y - s)/
res = 1;
for ( i = 1; i <= n; i <<= 1 ) // n = exponent
{
if ( n & i )
res *= a; // a = base
a *= a;
}
This should be more effective code for power and I don't know why this works.
First line of for() is fine I know why is there i <<= i. But I don't understand the line where is: if ( n & i ). I know how that works but I don't know why...
Let us say you have a binary representation of an unsigned number. How do you find the decimal representation?
Let us take a simple four bit example:
N = | 0 | 1 | 0 | 1 |
-----------------------------------------
| 2^3 = 8 | 2^2 = 4 | 2^1 = 2 | 2^0 = 1 |
-----------------------------------------
| 0 | 4 | 0 | 1 | N = 4 + 1 = 5
Now what would happen if the base wasn't fixed at 2 for each bit but instead was the square of the previous bit and you multiply the contribution from each bit instead of adding:
N = | 0 | 1 | 0 | 1 |
----------------------------
| a^8 | a^4 | a^2 | a^1 |
----------------------------
| 0 | a^4 | 0 | a^1 | N = a^4 * a^1 = a^(4+1) = a^5
As you can see, the code calculate a^N