DZone 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

Snippets

  • submit to reddit

Recent Snippets

                    one-liner to print out the current date & time given the time()

perl -le 'print scalar localtime shift'
                
                    <!--
*** str_repeat
*** Returns str repeated multiplier times
*** Param str = the string
*** Param multiplier = the string
-->
-
	<xsl:stylesheet version="1.0">
-
	<xsl:template match="/">
<xsl:param name="string" select="'Hello'"/>
-
	<xsl:call-template name="str_repeat">
<xsl:with-param name="str" select="$string"/>
<xsl:with-param name="multiplier" select="number(10)"/>
</xsl:call-template>
</xsl:template>
-
	<xsl:template name="str_repeat">
<xsl:param name="str"/>
<xsl:param name="multiplier"/>
<xsl:param name="count" select="number(1)"/>
-
	<xsl:if test="$count <= $multiplier">
-
	<xsl:call-template name="str_repeat">
<xsl:with-param name="str" select="$str"/>
<xsl:with-param name="multiplier" select="$multiplier"/>
<xsl:with-param name="count" select="$count+1"/>
</xsl:call-template>
<xsl:value-of select="$str"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>                
                    <!--
*** ucfirst
*** Make a string's first character uppercase
*** Param str = the string
-->
-
	<xsl:stylesheet version="1.0">
-
	<xsl:template match="/">
<xsl:param name="string" select="'the quick brown fox jumped over the lazy dog.'"/>
-
	<xsl:call-template name="ucfirst">
<xsl:with-param name="str" select="$string"/>
</xsl:call-template>
</xsl:template>
-
	<xsl:template name="ucfirst">
<xsl:param name="str"/>
<xsl:param name="strLen" select="string-length($str)"/>
<xsl:variable name="firstLetter" select="substring($str,1,1)"/>
<xsl:variable name="restString" select="substring($str,2,$strLen)"/>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="translate" select="translate($firstLetter,$lower,$upper)"/>
<xsl:value-of select="concat($translate,$restString)"/>
</xsl:template>
</xsl:stylesheet>                
                    def foobar(astring, bstring)
  Hash.new do |l,(i, j)|
    if l.key? [i, j]
      l[[i, j]]
    else
      a, b = astring[i], bstring[j]
      l[[i, j]] = case
      when !a, !b then  0
      when a == b then  1 + l[[i + 1, j + 1]]
      else              [ l[[i + 1, j]], l[[i, j + 1]] ].max
      end
    end
  end[[0, 0]]
end
                
                    Use this in your print style sheet. Works only in "better browsers".

a[href] {
  text-decoration: none;
      }

a[href]:after {
  content: " (" attr(href)") ";
       }
                
                    
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
                
                    We don't really want to delete them all just in case.

cd /usr/local/scratch/
mkdir junk
find /var/spool/postfix -exec grep "somediscernible-feature.com" '{}' \; | awk '{print($3)}' | xargs -J X mv X ./junk/

The "find" produces

Binary file /var/spool/postfix/active/D/D8832E38 matches
Binary file /var/spool/postfix/active/D/D78EC1C72 matches
Binary file /var/spool/postfix/active/D/D593D279D matches
Binary file /var/spool/postfix/active/D/D0EB32833 matches

The awk

/var/spool/postfix/active/D/D8832E38
/var/spool/postfix/active/D/D78EC1C72
/var/spool/postfix/active/D/D593D279D
/var/spool/postfix/active/D/D0EB32833

And then the mv, moves it.                
                    
class ApplicationController < ActionController::Base
  before_filter :choose_theme
  
  private
  def choose_theme
    ActionController::Base.template_root = File.join(RAILS_ROOT, 'app/views', @params['theme'])
  end
end
                
                    To always show the vertical scrollbar, regardless of whether there is content to scroll (à la Internet Explorer), add this to userContent.css

html {
     overflow: -moz-scrollbars-vertical;
}

Tested in Firefox 1.0 and above.                
                    To have a <DIV> shrink and enlarge based on its content, use this CSS:

div.autosize { display: table; width: 1px; }
div.autosize > div { display: table-cell; }

In your HTML, enclose the <DIV> you want autosized in another <DIV> with class="autosize"

Before:

<div>
I want this to be auto sized!
</div>

After:

<div class="autosize">
<div>
I want this to be auto sized!
</div>
</div>

Tested in Firefox, Mozilla, Safari, Opera 7, and Internet Explorer 6.