array_search result not getting properly - mysql

here i have two tables namely the rooms and student_hostel.
rooms look like this
id rm_number capacity bed_no class hostel is_vaccant
40 1 5 1A,1B,1C,1D,1E 27 7 1
41 2 4 2A,2B,2C,2D 28 7 1
42 3 3 3A,3B,3C 29 10 1
43 4 4 4A,4B,4C,4D 30 10 1
44 5 6 5A,5B,5C,5D,5E,5F 27 7 1
45 6 7 6A,6B,6C,6D,6E,6F,6G 29 10 1
student_hostel looks like this
id first_name stud_id hostel class room bed status
175 siraj WPGH00175 7 28 41 2A P
176 nesru WPGH00176 7 28 41 2B P
180 dsf WPGH00180 7 27 40 1A G
Here is the code that i tried to get all occupied rooms
the controller looks like this
public function beds_occupied($rm)
{
$data['bed'] = $this->admin_model->ajax_bed($rm)->row();
$data['beds'] = explode(',',$data['bed']->bed_no);
$data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();
foreach ($data['bed_no'] as $row)
{
if (($key = array_search($row->bed, $data['beds'])) !== false)
{
unset($data['beds'][$key]);
$data['beds'] = array_values($data['beds']);
}
}
return $data['beds'];
}
the model looks like this
public function ajax_bed($val=Null)
{
if(isset($val))
{
$this->db->where('id',$val);
}
return $this->db->get('rooms');
}
public function get_bed_no($id)
{
return $this->db->get_where('student_hostel',array('room'=>$id));
}
the ouput iam getting is all vaccant beds instead of occupied,
the result iam getting is like this.
SI no Rooms Class Hostel Occupied
1 1 periyar1 periyar 1B,1C,1D,1E
2 2 periyar2 periyar 2C,2D
3 3 pamba1 pamba 3A,3B,3C
4 4 pamba 2 pamba 4A,4B,4C,4D
5 5 periyar1 periyar 5A,5B,5C,5D,5E,5F
6 6 pamba1 pamba 6A,6B,6C,6D,6E,6F,6G
but i want to get it like this
SI no Rooms Class Hostel Occupied
1 1 periyar1 periyar 1A
2 2 periyar2 periyar 2A,2B
3 3 pamba1 pamba
4 4 pamba 2 pamba
5 5 periyar1 periyar
6 6 pamba1 pamba

You are unsetting the variables that DO exist. You want to keep the variables that DO exist as they are taken beds. Try this:
public function beds_occupied($rm)
{
$occupied = array();
$data['bed'] = $this->admin_model->ajax_bed($rm)->row();
$data['beds'] = explode(',',$data['bed']->bed_no);
$data['bed_no'] = $this->admin_model->get_bed_no($rm)->result();
foreach ($data['bed_no'] as $row)
{
$key = array_search($row->bed, $data['beds'];
if ($key)
{
$occupied[] = $data['beds'][$key]);
}
}
return $occupied;
}

Related

How to check for the rows continuity of an event in Google Sheet

I need help in Google Sheets to check for the continuity of an event which is a cell value say "3". The continuity needs to be checked for the last 7 cells. If the condition is satisfied (doesn't matter how many times) the value in the Result column is 1.
Please help in solving the problem.
Refer to the attached image for illustration.
Try in P2
=--ARRAYFORMULA(MAX((IFERROR({SPLIT(TEXTJOIN("|",TRUE,IF(A2:O2="",COLUMN(A2:N2),"")),"|"),16}-IFERROR({0,SPLIT(TEXTJOIN("|",TRUE,IF(A2:O2="",COLUMN(A2:N2),"")),"|")})-1)))>=7)
drag to bottom.
I've compared single cells with next, if different retrieve column number. Next create two array, shifiting the second to the right. The difference create the intervals of "3", using MAX check if exists the continuity that return TRUE or FALSE. The double minus transform that in 1 or 0 returning the value asked by OP
Continuity Checker
function continuityChecker() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2, 1, sh.getLastRow() -1, 15).getValues()
const o = vs.reduce((a, r, j) => {
r.forEach((c, i) => {
if (i == 0) { a.sum = 0; a.o.push([0]) }
if (c){ a.sum++;}else{a.sum = 0;}
if (a.sum == 7) a.o[a.o.length - 1] = [1];
})
return a;
}, { o: [], sum: 0, output: function () { return this.o; } }).output();
sh.getRange(2, 16, o.length, o[0].length).setValues(o);
}
Output:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Result
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
0
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1

How can I sort output from variable?

I want to be able to sort an input csv file that is comma separated by a values created in an extra column. Below is a sample of the input csv file
Timestamp,Email,Name,Year,Make,Model,Car_ID,Judge_ID,Judge_Name,Racer_Turbo,Racer_Supercharged,Racer_Performance,Racer_Horsepower,Car_Overall,Engine_Modifications,Engine_Performance,Engine_Chrome,Engine_Detailing,Engine_Cleanliness,Body_Frame_Undercarriage,Body_Frame_Suspension,Body_Frame_Chrome,Body_Frame_Detailing,Body_Frame_Cleanliness,Mods_Paint,Mods_Body,Mods_Wrap,Mods_Rims,Mods_Interior,Mods_Other,Mods_ICE,Mods_Aftermarket,Mods_WIP,Mods_Overall
8/5/2018 14:10,honoland13#japanpost.jp,Hernando,2015,Acura,TLX,48,J04,Bob,0,0,2,2,4,4,0,2,4,4,2,4,2,2,2,2,2,0,4,4,4,6,2,0,4
8/5/2018 15:11,nlighterness2q#umn.edu,Noel,2015,Jeep,Wrangler,124,J02,Carl,0,6,4,2,4,6,6,4,4,4,6,6,6,6,6,4,6,6,6,6,6,4,6,4,6
8/5/2018 17:10,eguest47#microsoft.com,Edan,2015,Lexus,Is250,222,J05,Adrian,0,0,0,0,0,0,0,0,6,6,6,0,0,6,6,6,0,0,0,0,0,0,0,0,4
8/5/2018 17:34,hchilley40#fema.gov,Hieronymus,1993,Honda,Civic eG,207,J06,Aaron,0,0,2,2,2,2,2,2,0,4,2,2,2,2,2,2,4,2,2,0,0,0,2,2,0
8/5/2018 14:30,nnowick3d#tuttocitta.it,Nickolas,2016,Ford,Mystang,167,J02,Carl,0,0,2,2,0,2,2,0,0,0,0,2,0,2,2,2,0,0,2,0,0,0,0,0,2
8/5/2018 16:12,mdearl39#amazon.co.uk,Martin,2013,Hyundai,Gen coupe,159,J04,Bob,0,0,2,0,0,0,2,0,0,0,0,2,0,2,2,0,2,0,2,0,0,0,0,0,0
8/5/2018 17:00,alynamg#blogtalkradio.com,Aldridge,2009,Infiniti,G37,20,J06,Aaron,2,0,2,2,0,0,2,0,0,2,2,2,2,2,2,2,2,2,4,2,2,0,2,0
What my code currently does is sift through the csv file, and pick out the car_id column, year, make, and model columns. Then it runs through every column from racer_turbo to the last, and for each row it adds up the values in those columns into a total value and prints that along side the other values (id, make, model, etc.). There is also a ranking column that precedes the other 5 when printed. Here is my code below.
BEGIN {
FS = ",";
OFS = "\t";
print "Ranking", "Car_ID", "Year", "Make", "Model", "Total";
}
{
rank;
total = 0;
if(NR > 1) {
for(i = 8; i < NF; i++) {
total += $i;
}
print ++rank,$7, $4, $5, $6, total;
}
rows[$5][total][$0]
}
END {
print "\n";
print "Ranking", "Car_ID", "Year", "Make", "Model", "Total";
ranking;
PROCINFO["sorted_in"] = "#ind_str_asc"
for (m in rows) {
n = asorti(rows[m], t, "#ind_num_desc");
n = (n>3) ? 3 : n
for(i = 1; i <= n; i++) for(s in rows[m][t[i]]) {
$0 = s;
$1 = ++r;
print ++ranking, $7, $4, $5, $6, total;
}
}
}
What I would like to do in the END block is print the output again, however, rank the cars by top three from each make using the total column which was created in the preceding block of the code. However, what I run my code now the output looks as follows
Ranking Car_ID Year Make Model Total
1 48 2015 Acura TLX 58
2 124 2015 Jeep Wrangler 118
3 222 2015 Lexus Is250 36
4 207 1993 Honda Civic eG 40
5 167 2016 Ford Mystang 18
6 159 2013 Hyundai Gen coupe 14
7 20 2009 Infiniti G37 36
...
Ranking Car_ID Year Make Model Total
1 113 2012 Acura Tsx sportwagon 10
2 112 2008 Acura TL 10
3 50 2015 Acura TLX 10
4 15 2014 Audi S4 10
5 18 2015 Audi S3 10
6 116 2008 Audi A4 10
7 2 2016 Bmw M2 10
8 172 2014 Bmw 4 10
9 28 1995 Bmw 318xi 10
...
See how in the total column on the second printed section it shows total is 10 for each printed car, instead of being the same values as they were in the first printed section for each respective car, and the highest 3 totals for each make being displayed.
Below is the expected output
Ranking Car_ID Year Make Model Total
1 48 2015 Acura TLX 58
2 124 2015 Jeep Wrangler 118
3 222 2015 Lexus Is250 36
4 207 1993 Honda Civic eG 40
5 167 2016 Ford Mystang 18
6 159 2013 Hyundai Gen coupe 14
7 20 2009 Infiniti G37 36
8 178 2009 Honda Oddesy 66
...
Ranking Car_ID Year Make Model Total
1 112 2008 Acura TL 110
2 50 2015 Acura TLX 102
3 127 2013 Acura Tsx 86
4 15 2014 Audi S4 120
5 18 2015 Audi S3 38
6 116 2008 Audi A4 28
7 2 2016 Bmw M2 24
8 172 2014 Bmw 4 22
9 111 2007 Bmw 328i 10
10 218 2010 Chevy Camaro 64
11 170 2014 Chevy Cruze 50
12 0 2015 Chevy Camaro 0
...
Is this salvagable with my current code? Or would a better approach be to create a separate awk file that will sort through the generated output and produce another file that is sorted by the top 3?
I'm running GNU AWK v4.0.2.
Assuming the Car_ID (hereinafter referred to as id) is unique across the rows, would you please try:
BEGIN {
FS = ","
OFS = "\t"
print "Ranking", "Car_ID", "Year", "Make", "Model", "Total"
}
{
rank
total = 0
if (NR > 1) {
for (i = 8; i < NF; i++) {
total += $i
}
print ++rank, $7, $4, $5, $6, total
ttl[$5][$7] = total
row[$7] = $0
}
}
END {
print "\n"
print "Ranking", "Car_ID", "Year", "Make", "Model", "Total"
ranking
id
PROCINFO["sorted_in"] = "#ind_str_asc"
for (m in ttl) {
n = asorti(ttl[m], t, "#val_num_desc")
n = (n>3) ? 3 : n
for (i = 1; i <= n; i++) {
id = t[i]
total = ttl[m][id]
$0 = row[id]
print ++ranking, $7, $4, $5, $6, total
}
}
}
I have slightly modified the data structure, assigning the id as the
main key. Then created a 2-D array ttl, which holds the value total
keyed by make and id. In the END loop, we can retrieve the
input data using the id.
As a side note, your original data structure uses total as an index.
If multiple rows with the same make happen to have the same value
of total, either of the indexes will be overwritten.

Importing Textfile as Matrix

i'm trying to import a txt-File to octave which contains a matrix of data.
The matrix looks like this:
49 ..1. ...1.......... ..... 49
47 ..12 ...1...... ... ..... 47
45 ..12....1...... 2....1... 45
43 ....2....1...... 2...1.... 43
41 .1..2.. .........2. .1..... 41
39 .1.12.2....1.....2. .1..... 39
37 .1..2.22...1.....2. .1..... 37
35 .1. 2222...2....2....1.1... 35
33 ....22.2...2....2....12.... 33
31 ....22.2...2..........21... 31
29 .....2.2...2.....2....21... 29
27 ........222222....2....21.... 27
25 .......22.2222....2.22.2..... 25
23 .......22.2222....2.2..2..... 23
21 .......222.222....2.2........ 21
19 ........22.222....2.......... 19
17 ..........2.2.2...22........... 17
15 ............................... 15
13 .......................2....... 13
11 .......................2......2 11
9 ........................2.....222 9
7 . ................. ..... ......222. 7
5 .................. ....1.. 5
3 ....... ......... .... 3
1 1
This is actually map/coordinate system (y-axis=azimuth, x-axis=latitude), which i have to plot. (blank=no data, .=no effects, 1=weak effects, 2=strong eff.)
The result should look like this
Because i failed to import this txt-File, i changed it into this.
49;1;1;1;1;1;1;1;2;2;3;2;1;2;2;2;3;2;2;2;2;2;2;2;2;2;2;1;2;2;2;2;2;1;1;1;1;1;1;1;49
47;1;1;1;1;1;1;1;2;2;3;4;1;2;2;2;3;2;2;2;2;2;2;1;2;2;2;1;2;2;2;2;2;1;1;1;1;1;1;1;47
45;1;1;1;1;1;1;1;2;2;3;4;2;2;2;2;3;2;2;2;2;2;2;1;4;2;2;2;2;3;2;2;2;1;1;1;1;1;1;1;45
43;1;1;1;1;1;1;2;2;2;2;4;2;2;2;2;3;2;2;2;2;2;2;1;4;2;2;2;3;2;2;2;2;1;1;1;1;1;1;1;43
39;1;1;1;1;1;1;2;3;2;3;4;2;4;2;2;2;2;3;2;2;2;2;2;4;2;1;2;3;2;2;2;2;2;1;1;1;1;1;1;39
37;1;1;1;1;1;1;2;3;2;2;4;2;4;4;2;2;2;3;2;2;2;2;2;4;2;1;2;3;2;2;2;2;2;1;1;1;1;1;1;37
and so on.
This is working with my code.
RawMap = dlmread('C:\Desktop\2576.map', ';', 0:80, 0:24)
Map = flipud(RawMap)
pcolor(Map(:,2:end-1))
To this automatically i don't want to the change the code. So i need to get the original file imported.
Any suggestions?
Thanks
Here is one approach to parse the file meaningfully:
S = fileread('testo.txt');
S = strsplit (S, "\n");
S = strvcat( S );
S = double(S);
S = S(:, 4:end-4);
S( S == double(" ") ) = 0;
S( S == double(".") ) = 1;
S( S == double("1") ) = 2;
S( S == double("2") ) = 3;
pcolor(S); axis ij;

get greatest Id from multiple table and remove duplicate value

i got some problem obtaining value from my db,this is my first db
ID_HARGA ID_USER ID_BAG_PEMASARAN ID_ITEM HARGA ENTRY_DATE
1 9 1 3 1000000 2015-01-11 09:55:27
2 9 1 5 2000000 2015-01-13 07:19:10
5 9 1 3 3000000 2015-01-13 13:47:32
6 9 1 43 7000000 2015-01-13 13:49:49
13 9 1 50 3000000 2015-01-13 17:56:54
37 9 1 50 100 2015-01-19 09:08:20
this is the second one
ID_ITEM NAMA_ITEM SPEC_ITEM MASA_GARANSI STATUS_ITEM DIR_IMAGE
3 water heater emas bagus sekali selamanya 1
5 water heater tembaga bagus super selamanya 1
43 water hater heater seupo 3 1 water_22.jpg
50 tankkk 50 Liter 1 1 water_2.jpg
i know there is some null value just igniore it, i already did this
public function get_item()
{
//menghitung jumlah varian barang yang ada
$querycounter = $this->db->query('select * from ITEM WHERE STATUS_ITEM = 1');
$counter = $querycounter->num_rows();
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
// $this->db->order_by('ENTRY_DATE','DESC');
//$this->db->limit($counter);
$this->db->where('STATUS_ITEM',1);
//$query = $this->db->query('select from ITEM i join HARGA h on i.ID_ITEM = h.ID_ITEM order by h.ENTRY_DATE ASC');
$query = $this->db->get();
return $query->result_array();
//$this->db->select('nama_item','spec_item');
//$query = $this->db->get('item');
}
what i want is,just ignore dir_image
ID_ITEM NAMA_ITEM HARGA DIR_IMAGE
3 water heater emas 1000000
5 water heater tembaga 3000000
43 water hater 7000000
50 tankk 100
ID_item with biggest number not the old , so everytime i am insert into id_harga with ID_ITEM
the result always the newest HARGA
Thx before
There is a little confession that, do you want to get the maximum hagra, or newest inserted data (identifiable by HAGRA_ID if its auto increment by 1) in Hagra table. Your Code should be like
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,max(HARGA),DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
$this->db->where('STATUS_ITEM',1);
$this->db->group_by('item.item_id');
This will join your tables and select maximum value of Hagra. To use Max function, you need to use group_by(CodeIgniter Documentation) clause, other wise it will show only the single maximum value. Group_by will help to show maximum value of each group (each item);
If you want to get the newest inserted data, you can use the same technique using
$this->db->select('MAX(HAGRA_ID),ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');

Subsetting in a function to calculate a row total

I have a data frame with results for certain instruments, and I want to create a new column which contains the totals of each row. Because I have different numbers of instruments each time I run an analysis on new data, I need a function to dynamically calculate the new column with the Row Total.
To simply my problem, here’s what my data frame looks like:
Type Value
1 A 10
2 A 15
3 A 20
4 A 25
5 B 30
6 B 40
7 B 50
8 B 60
9 B 70
10 B 80
11 B 90
My goal is to achieve the following:
A B Total
1 10 30 40
2 15 40 55
3 20 50 70
4 25 60 85
5 70 70
6 80 80
7 90 90
I’ve tried various method, but this way holds the most promise:
myList <- list(a = c(10, 15, 20, 25), b = c(30, 40, 50, 60, 70, 80, 90))
tmpDF <- data.frame(sapply(myList, '[', 1:max(sapply(myList, length))))
> tmpDF
a b
1 10 30
2 15 40
3 20 50
4 25 60
5 NA 70
6 NA 80
7 NA 90
totalSum <- rowSums(tmpDF)
totalSum <- data.frame(totalSum)
tmpDF <- cbind(tmpDF, totalSum)
> tmpDF
a b totalSum
1 10 30 40
2 15 40 55
3 20 50 70
4 25 60 85
5 NA 70 NA
6 NA 80 NA
7 NA 90 NA
Even though this way did succeeded in combining two data frames of different lengths, the ‘rowSums’ function gives the wrong values in this example. Besides that, my original data isn't in a list format, so I can't apply such a 'solution'.
I think I’m overcomplicating this problem, so I was wondering how can I …
Subset data from a data frame on the basis of ‘Type’,
Insert these individual subsets of different lengths into a new data frame,
Add an ‘Total’ column to this data frame which is the correct sum of the
individual subsets.
An added complication to this problem is that this needs to be done in an function or in an otherwise dynamic way, so that I don’t need to manually subset the dozens of ‘Types’ (A, B, C, and so on) in my data frame.
Here’s what I have so far, which doesn’t work, but illustrates the lines I’m thinking along:
TotalDf <- function(x){
tmpNumberOfTypes <- c(levels(x$Type))
for( i in tmpNumberOfTypes){
subSetofData <- subset(x, Type = i, select = Value)
if( i == 1) {
totalDf <- subSetOfData }
else{
totalDf <- cbind(totalDf, subSetofData)}
}
return(totalDf)
}
Thanks in advance for any thoughts or ideas on this,
Regards,
EDIT:
Thanks to the comment of Joris (see below) I got an end in the right direction, however, when trying to translate his solution to my data frame, I run into additional problems. His proposed answer works, and gives me the following (correct) sum of the values of A and B:
> tmp78 <- tapply(DF$value,DF$id,sum)
> tmp78
1 2 3 4 5 6
6 8 10 12 9 10
> data.frame(tmp78)
tmp78
1 6
2 8
3 10
4 12
5 9
6 10
However, when I try this solution on my data frame, it doesn’t work:
> subSetOfData <- copyOfTradesList[c(1:3,11:13),c(1,10)]
> subSetOfData
Instrument AccountValue
1 JPM 6997
2 JPM 7261
3 JPM 7545
11 KFT 6992
12 KFT 6944
13 KFT 7069
> unlist(sapply(rle(subSetOfData$Instrument)$lengths,function(x) 1:x))
Error in rle(subSetOfData$Instrument) : 'x' must be an atomic vector
> subSetOfData$InstrumentNumeric <- as.numeric(subSetOfData$Instrument)
> unlist(sapply(rle(subSetOfData$InstrumentNumeric)$lengths,function(x) 1:x))
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
> subSetOfData$id <- unlist(sapply(rle(subSetOfData$InstrumentNumeric)$lengths,function(x) 1:x))
Error in `$<-.data.frame`(`*tmp*`, "id", value = c(1L, 2L, 3L, 1L, 2L, :
replacement has 3 rows, data has 6
I have the disturbing idea that I’m going around in circles…
Two thoughts :
1) you could use na.rm=T in rowSums
2) How do you know which one has to go with which? You might add some indexing.
eg :
DF <- data.frame(
type=c(rep("A",4),rep("B",6)),
value = 1:10,
stringsAsFactors=F
)
DF$id <- unlist(lapply(rle(DF$type)$lengths,function(x) 1:x))
Now this allows you to easily tapply the sum on the original dataframe
tapply(DF$value,DF$id,sum)
And, more importantly, get your dataframe in the correct form :
> DF
type value id
1 A 1 1
2 A 2 2
3 A 3 3
4 A 4 4
5 B 5 1
6 B 6 2
7 B 7 3
8 B 8 4
9 B 9 5
10 B 10 6
> library(reshape)
> cast(DF,id~type)
id A B
1 1 1 5
2 2 2 6
3 3 3 7
4 4 4 8
5 5 NA 9
6 6 NA 10
TV <- data.frame(Type = c("A","A","A","A","B","B","B","B","B","B","B")
, Value = c(10,15,20,25,30,40,50,60,70,80,90)
, stringsAsFactors = FALSE)
# Added Type C for testing
# TV <- data.frame(Type = c("A","A","A","A","B","B","B","B","B","B","B", "C", "C", "C")
# , Value = c(10,15,20,25,30,40,50,60,70,80,90, 100, 150, 130)
# , stringsAsFactors = FALSE)
lnType <- with(TV, tapply(Value, Type, length))
lnType <- as.integer(lnType)
lnType
id <- unlist(mapply(FUN = rep_len, length.out = lnType, x = list(1:max(lnType))))
(TV <- cbind(id, TV))
require(reshape2)
tvWide <- dcast(TV, id ~ Type)
# Alternatively
# tvWide <- reshape(data = TV, direction = "wide", timevar = "Type", ids = c(id, Type))
tvWide <- subset(tvWide, select = -id)
# If you want something neat without the <NA>
# for(i in 1:ncol(tvWide)){
#
# if (is.na(tvWide[j,i])){
# tvWide[j,i] = 0
# }
#
# }
# }
tvWide
transform(tvWide, rowSum=rowSums(tvWide, na.rm = TRUE))