Printing a crude boxed message using perl – useful in shell too

Sometimes you would want to print  boxed messages such as the following from a script:
+-----------------------------------+
|A word is defined as a string of   |
|characters delimited by spaces,    |
|tabs, or newline characters.       |
+-----------------------------------+

If you want to have a re-usable routine to do this, things get a bit complex. There comes print formatting and word wrapping. While print formatting is easy in shell with printf, word wrapping would require writing a lot of lines of code and trickery. But you can do this real quick using Perl. Here is a little program.
To execute, pass string to be boxed and width of the box as commandline arguments as below

 perl box.pl "A word is defined as a string of characters delimited by spaces, tabs, or newline characters." 35

Source:

use Text::Wrap;
$Text::Wrap::columns = $ARGV[1];
print ( "+" . ( "-" x $ARGV[1] ) . "+\n" );
foreach(split('\n',wrap('', '', $ARGV[0])))
{
        printf "|%-$ARGV[1]s|\n", $_;
}
print ( "+" . ( "-" x $ARGV[1] ) . "+\n" );

Leave a Comment

Shameless plug

Here is my Google project to demo hibernate search features.
http://code.google.com/p/hb-search-demo/

Leave a Comment

Quick find and edit using vi in Linux/UNIX

OK, so you want to edit/view all the files matching certain pattern on vi from command prompt. How would you do it?
Search files using usual “find” command, then translate new line in result to space using “tr” command and feed the list to vi – simple! Here you go:

vi `find . -name *.sh  | tr '\n' ' '`

Leave a Comment

Scala does not have checked exceptions

Good! so, the following code will not impact your method signature

def doSomething(n:Int) = {
  if (n < 10) {
    throw new  Exception("Only values between 1 and 10")
  }
 ...

Leave a Comment

vim 7 spell check

vim as of version 7 had introduced built-in spellchecker. I had not used till date, but recently checked it out. So, to turn it on, type command
set spell
If you have some text on your vim and you turn sell on with above command, you will see red underlines for words that vim thinks are incorrectly spelled. Of course, while coding you might not want spell check, so you can turn it off by
set nospell
Now, about navigation and correction.
To go to next spelling error, the keyboard command is ]s to go to previous error, the command is [s. To see spelling suggestions, while the cursor is on word with error, press =z and it presents a list of possible words to choose from. Just type the number corresponding to the word that you are interested in, and you are all set.

Leave a Comment

Java Generics puzzler

Ok, so you are very excited about Generics and would “Generify” whatever you can. But here some interesting pointers.

One: This will cause a compilation error


 Set<String> set = new HashSet<String>();
 set.add("Testing");
 set.add("Testing again");
 set.add("Testing again and again");
 set.add(new Integer(1));

Okay, that we all know.

Two: Now, what will happen with this?


  public static void main(String[] args){
    Set<String> set = new HashSet<String>();
     set.add("Testing");
     set.add("Testing again");
     set.add("Testing again and again");
     doSomething(set);
     System.out.println(set);
   }
   private static void doSomething(Set s){
     s.add(new Integer(21));
   }
}

This will compile fine (With a warning actually, that we all would ignore) and print contents of the set, including a number.

Three: Well, how to fix this ? How to throw an error when a legacy code is attempting to change the sole purpose of your HashSet? The answer is to declare your set as

Set<String> set = Collections.checkedSet(new HashSet<String>(),String.class);

Leave a Comment

If you want to add an Object to java.util.TreeSet, it must implement Comparable

public class Employee{
  private String name;
  Employee(String str ){
    this.name= str;
  }
}
public class Test {
  public static void main(String argv[]){
    TreeSet myMap = new TreeSet();
    Employee s3 = new Employee("abcdef");
    Employee s4 = new Employee("abcdef");
    myMap.add(s3);
    myMap.add(s4);

This code will throw a
Exception in thread "main" java.lang.ClassCastException

Unless Employee implements comparable

Leave a Comment

Java Splitting string based on space

Quick and easy way to do this is :

stringVariable.split("\\s");

Leave a Comment

Hibernate Search ( with lucene) quick tip

By default, all the indexing done by lucene is stored in lower case. So, whenever constructing a WildCardQuery or TermQuery, remember to use smaller case matching criteria by default.
Also, never under-estimate luke, the lucene index toolbox

Leave a Comment

Maven POM

The Project Object Model (POM) describes :

  • Project co-ordinates (name, version..)
  • Project type (war,jar,ear..)
  • Source structure
  • Build Phases (standard | custom..)
  • Dependencies
  • Plug-ins

Leave a Comment

Older Posts »