December 8, 2009 at 4:13 pm
· Filed under Technology ·Tagged box, dialog, formatting, perl, shell
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" );
Permalink
November 17, 2009 at 4:04 pm
· Filed under Uncategorized ·Tagged edit, find, gvim, tr, vi, vim
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' ' '`
Permalink
July 31, 2009 at 8:46 am
· Filed under Technology
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")
}
...
Permalink
July 29, 2009 at 5:39 am
· Filed under Technology ·Tagged gvim, spellcheck, vim
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.
Permalink
July 20, 2009 at 9:22 am
· Filed under Technology
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);
Permalink
July 17, 2009 at 9:39 am
· Filed under Technology ·Tagged Comparable, java, TreeSet
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
Permalink
July 14, 2009 at 9:15 am
· Filed under Technology ·Tagged java, space, split, string
Quick and easy way to do this is :
stringVariable.split("\\s");
Permalink
July 14, 2009 at 7:55 am
· Filed under Uncategorized ·Tagged hibernate, hibernate search, java, lucene
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
Permalink
July 13, 2009 at 8:02 am
· Filed under Technology ·Tagged java, 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
Permalink
Older Posts »