Friday, December 23, 2011

JS: Word Count using Javascript

I just made a word counter using Javascript. Started it of as a jsfiddle attempt out of boredom. Thought it will help me count the words in assignments. blah. I have also made the source code available in github.

The counter is a simple javascript function, but have used jQuery selectors for simplicity and easiness. Initially my aim was to just get the count of words in the textarea by splitting the spaces and adding 1. But that approach is not accurate, when there are multiple spaces between words. So I used regular expression for replacing multiple spaces with a single space: /\s+/gi.

counter = function() {
    var value = $('#text').val();

    if (value.length == 0) {
        $('#wordCount').html(0);
        $('#totalChars').html(0);
        $('#charCount').html(0);
        $('#charCountNoSpace').html(0);
        return;
    }

    var regex = /\s+/gi;
    var wordCount = value.trim().replace(regex, ' ').split(' ').length;
    var totalChars = value.length;
    var charCount = value.trim().length;
    var charCountNoSpace = value.replace(regex, '').length;

    $('#wordCount').html(wordCount);
    $('#totalChars').html(totalChars);
    $('#charCount').html(charCount);
    $('#charCountNoSpace').html(charCountNoSpace);
};

The results are set in <span> elements as follows:
    0
    0
    0
    0

Now attach the counter function to change, keydown, keypress, keyup, blur and focus events of the textarea, to make sure the function called on every change.

$(document).ready(function() {    
    $('#text').change(counter);
    $('#text').keydown(counter);
    $('#text').keypress(counter);
    $('#text').keyup(counter);
    $('#text').blur(counter);
    $('#text').focus(counter);
});

This can be accessed in the following links

1. Word Counter using javascript

2. Source code for word counter using javascript

3. jsFiddle for word counter using javascript


Please report any bugs, issues or suggestions in github or just comment here.

Share it if you liked!

Wednesday, December 21, 2011

Oracle SQL to calculate value based on previous row value

Oracle database allows you to use SQL to calculate value based on the values of the previous row in a query result set. Use LAG and LEAD analytic function to calculate based on previous rows values.

Usage:
Both functions have the same usage, as shown below.
LAG (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)
LEAD (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)

Description:
value_expression - Can be a column or a built-in function, except for other analytic functions.
offset - The number of rows preceeding/following the current row, from which the data is to be retrieved. The default value is 1.
default - The value returned if the offset is outside the scope of the window. The default value is NULL.

Example:

select salary,lag(salary+300,1,0) over (order by salary) as new_sal,lag(salary+200,2,0) over (order by salary) as new_sal2 from employees;


   

Java: Marshalling in Java

Marshalling is a technique that can be easily understood and utilized efficiently. IT is the process of converting a POJO(Plain Old Java Object) in memory into a format that can be written to disk or send via network, usually in text formats like xml or json. The reverse of this technique is called unmarshalling.

Difference between Marshalling and Serialization:

Marshalling is similar to Serialization in practice but the difference is that, Marshalling also saves the code of an object in addition to its state.

Example

In the following example to explain marshalling, standard JAXB(Java Architecture for XML Binding) is used. For marshalling an object using JAXB, it must have annotation for the root element - @XmlRootElement
 
package blog.warfox.tutorials.marshalling;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * Student class for Marshalling demo example
 * @author warfox
 */
@XmlRootElement(name="student")

public class Student {

     private String name;
     private int rollNo;
     private double marks;
     private int rank;

     public String getName() {
          return this.name;
     }

     public void setName(String name) {
          this.name = name;
     }

     public int getRollNo() {
          return this.rollNo;
     }

     public void setRollNo(int rollNo) {
          this.rollNo = rollNo;
     }

     public double getMarks() {
          return this.marks;
     }

     public void setMarks(double marks) {
          this.marks = marks;
     }

     public int getRank() {
          return this.rank;
     }

     public void setRank(int rank) {
          this.rank = rank;
     }
}


Marshaller class which does the marshalling job

An instance of student is created and given for marshalling.
package blog.warfox.tutorials.marshalling;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class MyMarshaller {

     public static String marshall(Object object) throws JAXBException {

        JAXBContext context = JAXBContext.newInstance(object.getClass());
        
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter writer = new StringWriter();
        m.marshal(object, writer);

        return writer.toString();
    }

    public static void main(String[] args) throws JAXBException {

        Student student = new Student();
        student.setName("Name");
        student.setRollNo(1);
        student.setMarks(99.99);
        student.setRank(1);
  
        System.out.println(marshall(student));
   }
}

Output:


    99.99
    Name
    1
    1



In the above example, JAXB has automatically copied the xml tags from the property names of Student class. We can set our own tag names by using JAXB's @XmlElement annotation on the access method as follows.

package blog.warfox.tutorials.marshalling;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Student class for Marshalling demo example
 * @author warfox
 *
 */
@XmlRootElement(name="student")
public class Student {

   private String name;

   private int rollNo;

   private double marks;

   private int rank;

   @XmlElement(name="student-name") 
   public String getName() {
      return this.name;
   }

   public void setName(String name) {
     this.name = name;
   }

   @XmlElement(name="student-rollno")
   public int getRollNo() {
     return this.rollNo;
   }

   public void setRollNo(int rollNo) {
     this.rollNo = rollNo;
   }

   @XmlElement(name="student-marks")
   public double getMarks() {
     return this.marks;
   }

   public void setMarks(double marks) {
     this.marks = marks;
   }

   @XmlElement(name="student-rank")
   public int getRank() {
     return this.rank;
   }

   public void setRank(int rank) {
    this.rank = rank;
   }
}

Output:

    99.99
    Name
    1
    1


Share this tutorial if it helped you.
Related Posts Plugin for WordPress, Blogger...