Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS 

record grouping

@users = User.find(
  :all, 
  :select => "MONTH(created_at) as month, YEAR(created_at) as year, COUNT(*) AS records",
  :conditions => ["admin = 0'"], 
  :group => "year, month")

<% for @user in @users %>
  <%=@user.month%>
  <%=@user.year%>
  <%=@user.records%>
<% end %>

howmany

#!/usr/bin/perl
#==========================================================================================
# howmany -- a tool for determining how many different types of files are in a folder
#------------------------------------------------------------------------------------------
# Author: Elliot Winkler <elliot.winkler@gmail.com>
# Created: 11 Mar 2008
#==========================================================================================

my $dir = $ARGV[0] || ".";
my $cmd = "find $dir";
my @listing = sort grep { $_ } split /\n/, `$cmd`;

my %exts;
for (@listing) {
  my($ext) = /\.([a-z]+)$/;
  next unless $ext;
  $exts{lc $ext}++;
}

for (sort keys %exts) {
  print uc($_).": ".$exts{$_}."\n";
}

Example:

$ cd ~/docs
$ howmany
CSV: 3
DOC: 1
KEY: 1
PUB: 1
SQL: 1
TEXT: 1
TXT: 9
XCF: 2
XLS: 1
ZIP: 1
$ howmany ~/docs
CSV: 3
DOC: 1
KEY: 1
PUB: 1
SQL: 1
TEXT: 1
TXT: 9
XCF: 2
XLS: 1
ZIP: 1


Note: This only works on Linux/Unix, because it relies on a Linux/Unix-only command to pull up the list of files. A future update may include support for Windows, though it would be pretty easy to find out how to fix it.

Count the no of elements in an XML node

This Ruby code count the no. of elements in an xml node by converting the children into an array and then returning the length.

require 'rexml/document'
include REXML

file = File.new('test.xml')
doc = Document.new(file)
puts doc.root.elements.to_a.length

Count number of set bits in an integer

count = 0
count += byte & 1 and byte >>= 1 until byte == 0

Array group by count

Function for group by count of elements, with a optional parameter for sort on asc and desc mode

<?php
function ArrayGroupByCount($_array, $sort = false) {
   $count_array = array();

   foreach (array_unique($_array) as $value) {
       $count = 0;

		foreach ($_array as $element) {
		    if ($element == $value)
		        $count++;
		}
	
		$count_array[$value] = $count;
	}
	
	if ( $sort == 'desc' )
		arsort($count_array);
	elseif ( $sort == 'asc' )
		asort($count_array);

	return $count_array;
}
?>

sql count

// description of your code here

select count (*) as total from bppersondisabledstandardsetups group by setuptype;
select distinct bppersondisabledstandardsetups.setuptype from bppersondisabledstandardsetups;
select max(objid) from sdfields;

Bit Counting and clever loop condition

unsigned bit_count(unsigned x) {
    unsigned n;
    for (n = 0; x; n++)
        x &= x-1;
    return n;
}

Conteo de caracteres

Este pedazo de codigo cuenta los caracteres de un archivo

this code count the chars of a file.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int cont[257];
    int t;
    FILE *ent;
    
    for (t=0;t<257;t++)
        cont[t]=0;
      
    ent=fopen("input.txt","r");
//    printf("Se abrio el archivo :P\n");
    do {
        t = fgetc(ent);
        cont[t]++;
    } while (!feof(ent));
    fclose(ent);

    for (t=0;t<257;t++){
        printf("Caracter %i se repitio %i veces\n", t, cont[t]);
    }

    
//  system("PAUSE");	
  return 0;
}

classify() for alternating rows, columns, etc.

I often want different rows in a table to alternate in color, and I do this by assigning each row a class name and styling it with CSS. This is a simple helper method designed to return a class name based on the given row count.

It is also convenient for me to assign a class to table cells based on the type of content they hold. For example, if I have a cell with a float value in it, I want to display it with a monospace font, whereas if I have a cell with a string in it, I want to display it in a serif font.

# Determines the CSS class based on either the count given
# (returns 'even' or 'odd' as the CSS class name) or the class
# given (returns the string version of the class, lowercased,
# as the CSS class name)
def classify( count_or_class, include_class_text = true )
  if count_or_class.class == Fixnum
    value = ( count_or_class % 2 == 0 ? 'even' : 'odd' )
  else
    value = count_or_class.to_s.downcase
  end

  if include_class_text
    'class="' << value << '"'
  else
    value
  end
end


Example usage with alternating 'even'/'odd' row class names:
<table>
<% count = 0 %>
<% @collection.each do |value| %>
  <tr <%= classify( count ) %>>
    <td><%=h value %></td>
  </tr>
  <% count += 1 %>
<% end %>
</table>


Example usage with data type class names:
<tr>
<% @columns.each do |column| %>
  <% data = row.send( column.name ) %>
  <td <%= classify( data.class ) %>>
    <%=h data %>
  </td>
<% end %>
</tr>

Count Values in an Array

This modifies the Array class to allow for easy summing
class Array
  def total
    t = 0
    self.each { |v| t += v }
    t
  end
end

Use it like this:
[1,2,2,3].total  # => 8
« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS