JavaScript 在TextArea中写入文本

<head>
<script language="javascript" type="text/javascript">
	function addtext(text) {document.form.textarea.value = document.form.textarea.value+= text;}
	</script>
</head>

<body>
<form action="" method="" name="form">
   <textarea name="textarea" rows="" cols="" wrap="wrap"></textarea>
</form>
<a href="javascript:addtext('Text to add');">Add text</a>
</body>

ActionScript 安排MC从图书馆到舞台

private function attachThumbs (anzreihe:Number, anzgesamt:Number, itemwidth:Number, itemheight:Number, margin:Number) :Void {
		
		trace("attachThumbs");
		
		var anzahl_reihe:Number = anzreihe;
		var anzahl_gesamt:Number = anzgesamt;
		
		var mc_breite:Number = itemwidth;
		var mc_hoehe:Number = itemheight;
		
		var abstand:Number = margin*2;
		
		var x_wert:Number = 48;
		var y_wert:Number = 248;

		var x_start:Number = x_wert/2;
		var y_start:Number = y_wert/2;
		
		for (var i:Number=0; i<anzahl_gesamt; i++) {
			
			var initObj:Object = new Object();
			initObj.targetx = x_start+(i%anzahl_reihe)*(mc_breite+abstand);
			initObj.targety = y_start+Math.floor(i/anzahl_reihe)*(mc_hoehe+abstand);

			initObj._x = initObj.targetx;
			initObj._y = initObj.targety;
			
			initObj._alpha = 0;
			initObj.count = i;
			
			var tmpMC:MovieClip = UI.attachMovie("VideoThumb","videothumb"+i,i+3, initObj);
			tmpMC.loadThumbPic(imagepath+"thumb.png");
			
			tmpMC.alphaTo(100,1,'linear',i*0.1);
			//tmpMC.tween(["_alpha","_x","_y"],[100,initObj.targetx,initObj.targety],1,'easeOutQuad',i*0.1);
			
		}
	
	}

PHP 轻松调试变量,以查找tpl.php文件中可用的变量

/**
 */
function _phptemplate_variables($hook, $vars) {
  //...
  $vars['args'] = $vars; //TODO: remove this debug variable.
  return $vars;
}

PHP 允许按区域模板覆盖。

/**
 * Return a set of blocks available for the current user.
 * Add a per-region template override option
 */
function ogt_blocks($region) {
  $output = '';
  if ($list = block_list($region)) {
    $function = 'region_'. $region;
    if ($theme_function = theme_get_function($function)) {
      $blocks = array($list);
      //leave the buidling to the dedicated function
      $output = call_user_func_array($theme_function, $blocks);
    }
    else {
      foreach ($list as $key => $block) {
        //Build the string ourselves.
        $output .= theme('block', $block);
      }
    }
  }

  // Add any content assigned to this region through drupal_set_content() calls.
  $output .= drupal_get_content($region);

  return $output;
}

/**
 * Template/theme for the region named "siteinfo" (footer)
 *
 * @return void
 **/
function ogt_region_siteinfo($blocks)  {
  return _phptemplate_callback('region_siteinfo', array('blocks' => $blocks));
}


//-- in a templatefile region_siteinfo.tpl.php, put something like this:
<?php foreach ($blocks as $block => $block) : ?>
  <?php print theme('block', $block); ?>
<?php endforeach; ?>

Ruby 迭代合并排序

# Erik Kastner 2008-02-13 iterative merge sort in ruby
require 'rubygems'
require 'activesupport'

# iterative merge sort. given [10,9,8,7,6,5,4,3,2,1], first loop would do
# [10,9] => [9,10], [8,7] => [7,8]... etc second loop
# [9,10,7,8] => [7,8,9,10]... etc
def merge_sort(a)
  # a one element array is already sorted
  return a unless a.size > 1
  
  # first groups are like [[10], [9]]...
  merge_size = 2
  
  # loop until merge_size > array.size
  # you can also find the nearst power of 2, for 10 thats 16
  # that's (log(a.size) / log(2))**2
  loop do
    offset = 0
    a.in_groups_of(merge_size) do |sub_array|
      subs = sub_array.in_groups_of(merge_size / 2)
      sub_array.size.times do |i|
        next if i+offset >= a.size
        
        # after the sub elemnts are shifted off, sub arrays might be empty
        # or have nil elements
        if (subs[0].empty? || subs[0].first.nil?)
          a[i+offset] = subs[1].shift; next
        end
        if (subs[1].empty? || subs[1].first.nil?)
          a[i+offset] = subs[0].shift; next
        end
        
        # set a[i+offset] to the lesser of the first elements of the sub arrays
        # shift that off the sub array
        a[i+offset] = (subs[0].first < subs[1].first) ? subs[0].shift : subs[1].shift
      end
      offset += sub_array.size
    end
    break if merge_size > a.size
    merge_size *= 2
  end
  a
end

a = [10,9,8,7,6,5,4,3,2,1]
# a = (1..8).map{rand(200)}
puts "before: #{a.inspect}"
puts "after: " + merge_sort(a).inspect

Bash 在OSX上获取WLAN上的信息

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk '/avg/ || / SSID/ {printf $0 }' | awk '{ print "WLAN : " $6 ", S/N " $2 "/" $4 }'

XML 基线RSS XML

<?xml version="1.0" ?>
<rss version="2.0">
  <channel>
    <title>This is my first RSS feed!</title>
    <link>http://www.myWebSite.com</link>
    <description>I am testing out creating an RSS feed...</description>
 
    <item>
      <title>The First Item to Check Out</title>
      <link>http://www.myWebSite.com/story1.htm</link>
      <description>This is the first article on my Web site!</description>
    </item>

    <item>
      <title>The Second Item to Check Out</title>
      <link>http://www.myWebSite.com/story1.htm</link>
      <description>This is the second article on my Web site!</description>
    </item>

    ...
  </channel>
</rss>

Perl 日期功能

sub date_mysql2sec {
#takes: date in "yyyy-mm-dd hh:mm:ss" format (with some freedom)
#returns: date in seconds since 1970 format
    use Time::Local;# 'timelocal_nocheck';
    my $mysqldate = shift;
    $mysqldate =~ /(\d{4}).(\d{2}).(\d{2}).(\d{2}).(\d{2}).(\d{2})/;
    my ($sec,$min,$hour,$mday,$mon,$year) = ($6,$5,$4,$3,$2,$1);
    if ($mon != 0) {$mon--};
    return timelocal($sec,$min,$hour,$mday,$mon,$year);
}

sub date_sec2mysql {
#takes: date in seconds since 1970 format
#returns: date in yyyy-mm-dd hh:mm:ss format
    my $secdate = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($secdate);
    $year += 1900;
    $mon++;
    $mon = $mon < 10 ? "0$mon" : $mon;
    $mday = $mday < 10 ? "0$mday" : $mday;
    $sec = $sec < 10 ? "0$sec" : $sec;
    $min = $min < 10 ? "0$min" : $min;
    $hour = $hour < 10 ? "0$hour" : $hour;    
    return qq{$year-$mon-$mday $hour:$min:$sec};
}

sub date_mysql_now {
#Takes: nothing
#Returns: current date and time in yyyy-mm-dd hh:mm:ss format
    return date_sec2mysql(time);
}

Perl 基于MySQL的功能

sub db_connect {
    my ($dbname, $dbuser, $dbpass) = @_;
    my $dbh = DBI->connect("DBI:mysql:$dbname",$dbuser,$dbpass);
    #$dbh->do(qq{set character set 'utf8';});
    return $dbh;
}

sub do_sql {
# Takes: $dbh, $sql
# Returns: status
    my $dbh = shift || die "Database not connected!\n";
    my $sql = shift || die "Missing SQL statement???\n";
    return $dbh->do($sql);
}

sub execute_sql {
# Takes: $dbh, $sql
# Returns: $result_arrayref
    my $dbh = shift || die "Database not connected!\n";
    my $sql = shift || die "Missing SQL statement???\n";
    my $sth = $dbh->prepare($sql);
    $sth->execute;
    my $result = $sth->fetchall_arrayref({}); # {} => Return arrayref of hashrefs
    return $result;
}

sub do_insert {
#takes: $dbh, $table, $datahash
#returns: status
    my $dbh = shift || die "Database not connected!\n";
    my $table = shift || die "Missing table!\n";
    my $datahash = shift || die "Nothing to insert!\n";
    my $insert = "INSERT INTO $table (" . join(',', keys %$datahash) . ') VALUES (' . join(',', values %$datahash) . ');';
    return &do_sql($dbh, $insert);
}

Bash 下载y转换视频de YouTube

#!/bin/bash

if [ $# -lt 1 ]; then
echo "Uso: $0 "
exit 1
fi

ID=`echo $1 | cut -d= -f2 | cut -d\& -f1`
FILE="youtube-${ID}"
BASE_URL="http://youtube.com/get_video.php"

wget -O /tmp/${FILE} $1

if [ $? == 0 ]; then
T_PARAM=`grep '&t=' /tmp/${FILE} | head -n 1 | awk -F'&t=' '{print $2}' | cut -d\& -f 1`
VIDEO_URL="${BASE_URL}?video_id=${ID}&t=${T_PARAM}"

wget -O ${FILE}.flv $VIDEO_URL

if [ $? != 0 ]; then
rm -f ${FILE}.flv
exit 1
else
echo "Formato (avi , mpg o wmv): "
read formato
ffmpeg -i ${FILE}.flv ${FILE}.$formato
fi
fi

rm -f /tmp/${FILE}