Nested Article Spinner in Perl

On October 6, 2011, in Programing, by klepa
sub nested{
	my $text = shift;
	my $openingConstruct = '{';
	my $closingConstruct = '}';
	my $separator = '\|';

	srand;

    # If we have nothing to spin just exit (don't use a regexp)
    if($text !~ /$openingConstruct/) {
        return $text;
    }

    # Find the first whole match
    if($text =~ /$openingConstruct(.+?)$closingConstruct/s) {
		my $match = $1;

        # Only take the last block
        if(my $pos = rindex($match, $openingConstruct)) {
            $match = substr($match, $pos + length($openingConstruct));
        }

        # And spin it
        my @parts  = split(/$separator/, $match);

		# And replace
		my $regex = quotemeta("${openingConstruct}${match}${closingConstruct}");
        $text  =~ s/$regex/$parts[int(rand($#parts+1))]/s;

        # We need to continue until there is nothing left to spin
        return &nested($text);
    } else {
        # If we have nothing to spin just exit
        return $text;
    }
}
Tagged with:  

My Beautiful Nehalennia

On July 7, 2011, in Personal, by klepa

My beautiful cedar strip canoe! This 16′ Canoe has been refined to the point of perfection. The planked hull is made from select pieces of British Columbia Red Cedar. Over time the reddish tint will darken considerably creating a remarkable contrast between it, the brass tacks, and the Eastern White Cedar ribs. Each hand built hull has a distinct pattern created by the variances in colour found in the planking.  “There is simply nothing that compares”… This is the constant refrain heard from anyone lucky enough to have paddled the worlds finest 16 footer.

Tagged with:  

UTF8 Tips & Tricks

On April 14, 2011, in Programing, by klepa

How output UTF8 from perl script

Add this to the script, before your print() statement:

binmode(STDOUT, ":utf8");

UTF8 in MySQL

Set in my.cnf default-character-set=utf8 for [client] and [mysqld]
To convert existing table use following command:

ALTER TABLE table CONVERT TO CHARACTER SET utf8;

UTF8 support in console

Add into your .profile or .bash_profile following:

export LC_ALL=en_US.UTF-8
Tagged with:  

Here’s fast script for building/re-building nested set/tree from adjacency list model. If you’re using cakephp this script is much faster then cakephp recover method on large data sets.

<?php
set_time_limit (0);

$con = mysql_connect("localhost","test","test");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("test", $con);

function rebuild_tree($parent_id, $left) {
	print "$leftn";
	// the right value of this node is the left value + 1
	$right = $left+1;

	// get all children of this node
	$result = mysql_query('SELECT id FROM categories '.
	'WHERE parent_id="'.$parent_id.'";');

	while ($row = mysql_fetch_array($result)) {
		// recursive execution of this function for each
		// child of this node
		// $right is the current right value, which is
		// incremented by the rebuild_tree function
		$right = rebuild_tree($row['id'], $right);
	}

	// we've got the left value, and now that we've processed
	// the children of this node we also know the right value
	mysql_query('UPDATE categories SET lft='.$left.', rght='.
	$right.' WHERE id="'.$parent_id.'";');

	// return the right value of this node + 1
	return $right+1;
}

rebuild_tree(1,1);
?>
Tagged with:  

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

    Archives

    All entries, chronologically...

    Weboy