Never been to DZone Snippets before?

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

About this user

Nilesh http://nilesh.org/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Perl: Dirify string

Clean up a string for creation of a directory named using the string.

my $dir = dirify('The s^#%@$!#! <b>title</b> ever');     # outputs 'The-S-Title-Ever'

sub dirify {
    my $s = shift;

    $s =~ s!<[^>]+>!!gs;            # Remove HTML tags.
    $s =~ s!<!&lt;!gs;

    $s =~ s!&[^;\s]+;!!g;           # Remove HTML entities.
    $s =~ s![^A-Za-z0-9-_ ]! !g;    # Allow only alphanumeric chars.
    $s =~ s!\s+! !g;                # Remove extra spaces.
    $s =~ s!\s$!!g;         
    $s =  lc $s;                    # Convert to lower-case.   
    $s =~ s!(\b.)!\U$1!g;           # Capitalize words (Comment out if you don't need this)
    $s =~ tr! !-!s;                 # Finally, change space chars to dashes.

    return $s;
}

Perl: Sorted union of two arrays

Create a union of two arrays and sort the array.

my @union = array_union_sort(\@first, \@second);

sub array_union_sort {
    my $aa = shift;
    my $bb = shift;
    my %union;
    my $e;
    foreach $e (@$aa) { $union{$e} = 1; }
    foreach $e (@$bb) { $union{$e} = 1; }
    my @union = keys %union;
    @union = sort { $a <=> $b } @union;
    return @union;
}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS