In mongo shell how would I delete all occurrences of "id" : "1" the value of the field is always different. Would I use the $unset operator? Would that delete the value and the field?
Monday, May 14, 2012
Mongo DB Delete a field and value
Javascript Phone number validation Paratheses sign
I did some searching and there where others asking this question and answers to it but none that seemed to fit what I was trying to do. Basically I'm working on a validation of the phone entry that accepts (123)4567890 as an entry. I've already implemented one that accepts a simple number string such as 1234567890 and one with dashes 123-456-7890. I know I'm making a simple mistake somewehre but I can't figure out what I'm doing wrong.
Here's the phone number with dashes form that is working:
//Validates phone number with dashes.
function isTwelveAndDashes(phone) {
if (phone.length != 12) return false;
var pass = true;
for (var i = 0; i < phone.length; i++) {
var c = phone.charAt(i);
if (i == 3 || i == 7) {
if (c != '-') {
pass = false;
}
}
else {
if (!isDigit(c)) {
pass = false;
}
}
}
return pass;
}?
and this is the one I can't manage to work out.
function isTwelveAndPara(phone) {
if (phone.length != 12) return false;
var pass = true;
for (var i = 0; i < phone.length; i++) {
var c = phone.charAt(i);
if (i == 0) {
if (c != '(') {
pass = false;
}
}
if (i == 4) {
if (c != ')') {
pass = false;
}
}
else {
if (!isDigit(c)) {
pass = false;
}
}
}
return pass;
}?
using perl hashes to handle tab-delimited files
I have two files: file 1 has 3 columns(SNP, Chromosome,position) and file 2 has 3 columns(Chromosome, peak_start and peak_end). All columns are numeric except for the SNP column.
If chromosome in file 1 matches file 2, i want to check if the position of the SNP lies in between peak_start and peak_end. If yes, show which SNP falls in which peak (preferably write output to a tab-delimited file). I would prefer to split the file, and use hashes where the chromosome is the key. I have found only a few questions remotely similar to this but i could not understand well the suggested solutions.
Here is the example of my code. It is only meant to illustrate my question and so far doesn't do anything so think of it as "pseudocode". Also, please forgive the formatting, this is my first time to ask a question here and i am a biologist trying to learn perl programming!
#!usr/bin/perl
use strict;
use warnings;
my (%peaks, %X81_05);
my @array;
#open file or die
unless (open (FIRST_SAMPLE, "X81_05.txt")) {
die "could not open X81_05.txt";
}
#split the tab-delimited file into respective fields
while (<FIRST_SAMPLE>) {
chomp $_;
next if (m/Chromosome/); #skip the header
@array = split("\t", $_);
($chr1,$pos,$sample) = @array;
$X81_05{'$array[0]'} = (
'position' =>'$array[1]'
)
}
close (FIRST_SAMPLE);
#open file using file handle
unless (open (PEAKS, "peaks.txt")) {
die "could not open peaks.txt";
}
my ($chr, $peak_start, $peak_end);
while (<PEAKS>) {
chomp $_;
next if (m/Chromosome/); #skip header
($chr, $peak_start, $peak_end) = split(/\t/);
$peaks{$chr}{'peak_start'} = $peak_start;
$peaks{$chr}{'peak_end'} = $peak_end;
}
close (PEAKS);
for my $chr1 (keys %X81_05) {
my $val = $X81_05{$chr1}{'position'};
for my $chr (keys %peaks) {
my $min = $peaks{$chr}{'peak_start'};
my $max = $peaks{$chr}{'peak_end'};
if ( ($val > $min) and ($val < $max) ) {
#print $val, " ", "lies between"," ", $min, " ", "and", " ", $max, "\n";
}else {
#print $val, " ", "does not lie between"," ", $min, " ", "and", " ", $max, "\n";
}
}
}
more awesome code
Rails server says port already used, how to kill that process?
I'm on a mac, doing:
rails server
I get:
2010-12-17 12:35:15] INFO WEBrick 1.3.1
[2010-12-17 12:35:15] INFO ruby 1.8.7 (2010-08-16) [i686-darwin10.4.0]
[2010-12-17 12:35:15] WARN TCPServer Error: Address already in use - bind(2)
Exiting
I know I can start one on a new port, but I want to kill this process.
Nested list back button sencha touch 2
I need to get value of back button of in nested list sencha touch 2 .. but it returns only back because it set default to back .. is there any way to get the actual value of it ?
make sure img div loads before video div
I have a page where when you click one out of a set of buttons, and image pops up and then fades as a video plays. The problem is that it works on the first click, but on the second on, the div with the image loads after the video loads. How can I make sure it appears before the video starts to load? the script (simplified) is
$('li, .thumbs').on('click', function() {
myVideo.load();
myVideo.play();
$('#myVid').bind("loadstart", function() {
$('#MyT').fadeIn(0);
});
$('#myVid').bind("playing", function() {
$('#MyT').fadeOut(500);
});
});?
select from one table where count=1 in second table and two more conditions
I need to select transactions with cash payment and same date but only for those transactions
which have only one payment ( so num 14 should be omitted from resultset )
So correct result is 12 and 13 only.
Table2 Table1
num | date | data | total num | payment | date
12 xy abc 2.5 12 cash xy
13 xy cbc 2.1 13 cash xy
14 xy acc 2.3 14 visa xy
19 xy def 2.0 14 cash xy
27 xy fgh 1.3 19 visa xy
27 mc xy
Something like this gives num 14 in result-set but 14 should be omitted.
SELECT num, data
FROM Table2
WHERE num IN
(
SELECT num FROM `Table1`
WHERE payment = 'cash'
GROUP BY `num`
HAVING ( COUNT(`num`) = 1 )
)
To sumarize correct answer (by tombom ):
SELECT t2.num, t2.data
FROM Table1 as t1
INNER JOIN Table2 as t2 ON t1.num = t2.num
AND t1.date = 'xy'
GROUP BY t1.num
HAVING GROUP_CONCAT(t1.payment) = 'cash'
Thanks!