Monday, November 28, 2005

Decimal to Binary

Crazy...

I've always had access to Perl (well since I started using Linux in 1994) and yet I've laboured over writing things in C sometimes... like maths stuff! - fancy spending days writing and months tweaking a command line calculator program in C... now I have this *massive* perl script:


#!/usr/bin/perl -w
die "Usage: $0 MATHS\n" unless(@ARGV);for(@ARGV){s/x/*/g};
print eval(join('',@ARGV)),$/;


To convert a decimal number to binary in perl you could do:


unpack("B32", pack("N", $number));


and then chopping off the leading zeros... as per http://www.unix.org.ua/orelly/perl/cookbook/ch02_05.htm

... but TIMTOWTDI, you're better to use:


sprint("%08b", $number);

Wednesday, November 23, 2005

Multi line comments in Perl...

How-can-I-comment-out-a-large-block-of-Perl-code?

or even just...


=pod
blah
blah
blah
~
~
~
=cut


...from http://mail.pm.org/pipermail/nwarkansas-pm/2000-September/000001.html


serf 2005-11-23 14:02:41-05 is =pod ... =cut still OK? or is that deprecated?
serf 2005-11-23 14:03:28-05 I found it here
ysth 2005-11-23 14:08:50-05 serf that will confuse any pod parser trying to look for documentation interspersed with your code; =for comment...=end comment =cut is better


http://perldoc.perl.org/perlpod.html

Prompt...

ksh and bash together:


PS_LOG_HOST="$LOGNAME@`hostname`:"
PS_TTY="[`tty|cut -c 6-`]"
case "$TERM" in
"xterm" | "screen" )
     BEL=`printf "\a"`
     if [ -n "${BASH_VERSION}" ]
     then
           PS1='\e]0;\u@\h:\w ${PS_TTY}\a\u@\h:\w ${PS_TTY}\n\$ '
     else
           PS0=`printf "\033]0;"`
           PS_KSH=`printf " (\033[01;31mksh\033[00m)"`
           PS1=$PS0$PS_LOG_HOST'$PWD '$PS_TTY$BEL$PS_LOG_HOST'$PWD '$PS_TTY$PS_KSH"
\$ "
     fi
;;
* )
     PS1=$LOG_HOST'$PWD$ '
;;
esac


You could trim the tty with ${MY_TTY:5}
but that substitution fails on ksh88:


ksh: ${MY_TTY:5}: bad substitution


bash:

case $TERM in
xterm )
     MYTTY=$(tty)
     PS1='\e]0;\u@\h:\w [${MYTTY:5}]\a\u@\h:\w [${MYTTY:5}]\n\$ '
;;
* )
     PS1='\u@\h:\w\n\$ '
;;
esac


ksh:


PS_LOG_HOST="$LOGNAME@`hostname`:"
case "$TERM" in
"xterm" | "screen" )
     BEL=`printf "\a"`
     PS0=`printf "\033]0;"`
     PS_TTY="[`tty|cut -c 6-`]"
     PS_KSH=`printf " (\033[01;31mksh\033[00m)"`
     PS1=$PS0$PS_LOG_HOST'$PWD '$PS_TTY$BEL$PS_LOG_HOST'$PWD '$PS_TTY$PS_KSH"
\$ "
;;
* )
     PS1=$PS_LOG_HOST'$PWD$ '
;;
esac

Tuesday, November 22, 2005

Supress count in isql

To supress "X rows affected" from printing with isql, do:


set nocount on


before the select

Tuesday, November 08, 2005

Excel keyboard shortcuts...

The one I wanted to know was to delete a cell (or a column or row)... because <Delete> just clears the cell contents...

The keyboard shortcut is <Ctrl--> (Control+Minus) (Hyphen)

http://www.ozgrid.com/Excel/ExcelKeyBoardShortcutKeys.htm

Monday, November 07, 2005

Replacing with ^M in vim

If you want to remove ^M (<Ctrl-M> or Carriage Return) in vim (or vi) you can just do:


s/^M//g


To get the ^M use ^V^M

If you do


s/<TEXT>/^M/g


the <TEXT> is replaced with a newline character (i.e. ^J or \n) instead :o(

If you want to replace text with an actual ^M do:


s/<TEXT>/\^M/g


Of course if you're on Windows using gvim the ^V^M doesn't work, it's just the same as pressing -
on Windows you need to use CTRL-Q instead of CTRL-V to escape the special character on the ex command line or to do a blockwise visual selection

From the documentation:

(usr_24.txt)

Note:
On MS-Windows CTRL-V is used to paste text. Use CTRL-Q instead of
CTRL-V. On Unix, on the other hand, CTRL-Q does not work on some
terminals, because it has a special meaning.

(gui_w32.txt)
*CTRL-V-alternative* *CTRL-Q*
Since CTRL-V is used to paste, you can't use it to start a blockwise Visual
selection. You can use CTRL-Q instead. You can also use CTRL-Q in Insert
mode and Command-line mode to get the old meaning of CTRL-V. But CTRL-Q
doesn't work for terminals when it's used for control flow.