Matthew van Eerde's web log

  • Matthew van Eerde's web log

    Generating sample first names


    I had a need to write a script that would give me a random first name.  I grabbed the top 200 first names for baby boys in the US from 2000-2009, and the same list for baby girls:

    Boys Girls
    Jacob Emily
    Michael Madison
    ... ...

    My initial implementation just printed out the name, but I quickly realized I needed to print out the gender if I wanted to talk about what the (fictitious) person did.  So I updated it to print out the gender as well.

    In the course of this I realized that some names appeared on both lists.  In particular they are:

    • Alexis
    • Angel
    • Jordan
    • Peyton
    • Riley

    The script is called like this:

    >perl -w
    Wesley (male)

    And here's the source:

    use strict;

    # prints a randomly chosen name

    sub read_words();

    my @words = read_words();
    print $words[ rand(@words) ];

    sub read_words() {
        my @words = <DATA>;

        chomp @words;

        return @words;
    Aaliyah (female)
    Aaron (male)
    Abby (female)
    Abigail (female)
    Abraham (male)
    Adam (male)
    Addison (female)
    Adrian (male)
    Adriana (female)
    Adrianna (female)
    Aidan (male)
    Aiden (male)
    Alan (male)
    Alana (female)
    Alejandro (male)
    Alex (male)
    Alexa (female)
    Alexander (male)
    Alexandra (female)
    Alexandria (female)
    Alexia (female)
    Alexis (female)
    Alexis (male)
    Alicia (female)
    Allison (female)
    Alondra (female)
    Alyssa (female)
    Amanda (female)
    Amber (female)
    Amelia (female)
    Amy (female)
    Ana (female)
    Andrea (female)
    Andres (male)
    Andrew (male)
    Angel (female)
    Angel (male)
    Angela (female)
    Angelica (female)
    Angelina (female)
    Anna (female)
    Anthony (male)
    Antonio (male)
    Ariana (female)
    Arianna (female)
    Ashley (female)
    Ashlyn (female)
    Ashton (male)
    Aubrey (female)
    Audrey (female)
    Austin (male)
    Autumn (female)
    Ava (female)
    Avery (female)
    Ayden (male)
    Bailey (female)
    Benjamin (male)
    Bianca (female)
    Blake (male)
    Braden (male)
    Bradley (male)
    Brady (male)
    Brandon (male)
    Brayden (male)
    Breanna (female)
    Brendan (male)
    Brian (male)
    Briana (female)
    Brianna (female)
    Brittany (female)
    Brody (male)
    Brooke (female)
    Brooklyn (female)
    Bryan (male)
    Bryce (male)
    Bryson (male)
    Caden (male)
    Caitlin (female)
    Caitlyn (female)
    Caleb (male)
    Cameron (male)
    Camila (female)
    Carlos (male)
    Caroline (female)
    Carson (male)
    Carter (male)
    Cassandra (female)
    Cassidy (female)
    Catherine (female)
    Cesar (male)
    Charles (male)
    Charlotte (female)
    Chase (male)
    Chelsea (female)
    Cheyenne (female)
    Chloe (female)
    Christian (male)
    Christina (female)
    Christopher (male)
    Claire (female)
    Cody (male)
    Colby (male)
    Cole (male)
    Colin (male)
    Collin (male)
    Colton (male)
    Conner (male)
    Connor (male)
    Cooper (male)
    Courtney (female)
    Cristian (male)
    Crystal (female)
    Daisy (female)
    Dakota (male)
    Dalton (male)
    Damian (male)
    Daniel (male)
    Daniela (female)
    Danielle (female)
    David (male)
    Delaney (female)
    Derek (male)
    Destiny (female)
    Devin (male)
    Devon (male)
    Diana (female)
    Diego (male)
    Dominic (male)
    Donovan (male)
    Dylan (male)
    Edgar (male)
    Eduardo (male)
    Edward (male)
    Edwin (male)
    Eli (male)
    Elias (male)
    Elijah (male)
    Elizabeth (female)
    Ella (female)
    Ellie (female)
    Emily (female)
    Emma (female)
    Emmanuel (male)
    Eric (male)
    Erica (female)
    Erick (male)
    Erik (male)
    Erin (female)
    Ethan (male)
    Eva (female)
    Evan (male)
    Evelyn (female)
    Faith (female)
    Fernando (male)
    Francisco (male)
    Gabriel (male)
    Gabriela (female)
    Gabriella (female)
    Gabrielle (female)
    Gage (male)
    Garrett (male)
    Gavin (male)
    Genesis (female)
    George (male)
    Gianna (female)
    Giovanni (male)
    Giselle (female)
    Grace (female)
    Gracie (female)
    Grant (male)
    Gregory (male)
    Hailey (female)
    Haley (female)
    Hannah (female)
    Hayden (male)
    Hector (male)
    Henry (male)
    Hope (female)
    Hunter (male)
    Ian (male)
    Isaac (male)
    Isabel (female)
    Isabella (female)
    Isabelle (female)
    Isaiah (male)
    Ivan (male)
    Jack (male)
    Jackson (male)
    Jacob (male)
    Jacqueline (female)
    Jada (female)
    Jade (female)
    Jaden (male)
    Jake (male)
    Jalen (male)
    James (male)
    Jared (male)
    Jasmin (female)
    Jasmine (female)
    Jason (male)
    Javier (male)
    Jayden (male)
    Jayla (female)
    Jazmin (female)
    Jeffrey (male)
    Jenna (female)
    Jennifer (female)
    Jeremiah (male)
    Jeremy (male)
    Jesse (male)
    Jessica (female)
    Jesus (male)
    Jillian (female)
    Jocelyn (female)
    Joel (male)
    John (male)
    Johnathan (male)
    Jonah (male)
    Jonathan (male)
    Jordan (female)
    Jordan (male)
    Jordyn (female)
    Jorge (male)
    Jose (male)
    Joseph (male)
    Joshua (male)
    Josiah (male)
    Juan (male)
    Julia (female)
    Julian (male)
    Juliana (female)
    Justin (male)
    Kaden (male)
    Kaitlyn (female)
    Kaleb (male)
    Karen (female)
    Karina (female)
    Kate (female)
    Katelyn (female)
    Katherine (female)
    Kathryn (female)
    Katie (female)
    Kayla (female)
    Kaylee (female)
    Kelly (female)
    Kelsey (female)
    Kendall (female)
    Kennedy (female)
    Kenneth (male)
    Kevin (male)
    Kiara (female)
    Kimberly (female)
    Kyle (male)
    Kylee (female)
    Kylie (female)
    Landon (male)
    Laura (female)
    Lauren (female)
    Layla (female)
    Leah (female)
    Leonardo (male)
    Leslie (female)
    Levi (male)
    Liam (male)
    Liliana (female)
    Lillian (female)
    Lilly (female)
    Lily (female)
    Lindsey (female)
    Logan (male)
    Lucas (male)
    Lucy (female)
    Luis (male)
    Luke (male)
    Lydia (female)
    Mackenzie (female)
    Madeline (female)
    Madelyn (female)
    Madison (female)
    Makayla (female)
    Makenzie (female)
    Malachi (male)
    Manuel (male)
    Marco (male)
    Marcus (male)
    Margaret (female)
    Maria (female)
    Mariah (female)
    Mario (male)
    Marissa (female)
    Mark (male)
    Martin (male)
    Mary (female)
    Mason (male)
    Matthew (male)
    Max (male)
    Maxwell (male)
    Maya (female)
    Mckenzie (female)
    Megan (female)
    Melanie (female)
    Melissa (female)
    Mia (female)
    Micah (male)
    Michael (male)
    Michelle (female)
    Miguel (male)
    Mikayla (female)
    Miranda (female)
    Molly (female)
    Morgan (female)
    Mya (female)
    Naomi (female)
    Natalia (female)
    Natalie (female)
    Nathan (male)
    Nathaniel (male)
    Nevaeh (female)
    Nicholas (male)
    Nicolas (male)
    Nicole (female)
    Noah (male)
    Nolan (male)
    Oliver (male)
    Olivia (female)
    Omar (male)
    Oscar (male)
    Owen (male)
    Paige (female)
    Parker (male)
    Patrick (male)
    Paul (male)
    Payton (female)
    Peter (male)
    Peyton (female)
    Peyton (male)
    Preston (male)
    Rachel (female)
    Raymond (male)
    Reagan (female)
    Rebecca (female)
    Ricardo (male)
    Richard (male)
    Riley (female)
    Riley (male)
    Robert (male)
    Ruby (female)
    Ryan (male)
    Rylee (female)
    Sabrina (female)
    Sadie (female)
    Samantha (female)
    Samuel (male)
    Sara (female)
    Sarah (female)
    Savannah (female)
    Sean (male)
    Sebastian (male)
    Serenity (female)
    Sergio (male)
    Seth (male)
    Shane (male)
    Shawn (male)
    Shelby (female)
    Sierra (female)
    Skylar (female)
    Sofia (female)
    Sophia (female)
    Sophie (female)
    Spencer (male)
    Stephanie (female)
    Stephen (male)
    Steven (male)
    Summer (female)
    Sydney (female)
    Tanner (male)
    Taylor (female)
    Thomas (male)
    Tiffany (female)
    Timothy (male)
    Travis (male)
    Trenton (male)
    Trevor (male)
    Trinity (female)
    Tristan (male)
    Tyler (male)
    Valeria (female)
    Valerie (female)
    Vanessa (female)
    Veronica (female)
    Victor (male)
    Victoria (female)
    Vincent (male)
    Wesley (male)
    William (male)
    Wyatt (male)
    Xavier (male)
    Zachary (male)
    Zoe (female)
    Zoey (female)

  • Matthew van Eerde's web log

    Programmatically adding a folder to a shell library (e.g., the Music library)


    I wrote a selfhost tool which allows me to add a folder (for example, C:\music) to a shell library (for example, the Music library.)

    This was before I found out about the shlib shell library sample which Raymond Chen blogged about.  If you're looking for a sample on how to manipulate shell libraries, prefer that one to this.


    pShellLibrary = SHLoadLibraryFromKnownFolder(library GUID)
    SHAddFolderPathToLibrary(pShellLibrary, path)


    shelllibrary add <path> to <library>
        <path> must already exist
        <library> must be one of:
            recorded tv
    >shelllibrary add C:\music to Music
    Added C:\music to Music library

    Source and binaries attached.

  • Matthew van Eerde's web log

    Changing the desktop wallpaper using IDesktopWallpaper


    About a year ago I wrote about how to change the desktop wallpaper using SystemParametersInfo(SPI_SETDESKWALLPAPER).

    Windows 8 desktop apps (not Store apps) can use the new IDesktopWallpaper API to get a more fine level of control.  So I wrote an app which uses the new API, though I just set the background on all monitors to the same image path, and I don't exercise any of the advanced features of the API.


    pDesktopWallpaper->SetWallpaper(NULL, full-path-to-image-file)


    >desktopwallpaper.exe "%userprofile%\pictures\theda-bara.bmp"
    Setting the desktop wallpaper to C:\Users\MatEer\pictures\theda-bara.bmp succeeded.

    Source and binaries attached

  • Matthew van Eerde's web log

    Grabbing large amounts of text from STDIN in O(n) time


    Last time I blogged about an O(n log n) solution to finding the longest duplicated substring in a given piece of text; I have since found an O(n) algorithm, which I linked to in the comments.

    But my blog post used an O(n2) algorithm to read the text from STDIN! It looked something like this:

    while (!done) {
        grab 2 KB of text
        allocate a new buffer which is 2 KB bigger
        copy the old text and the new text together into the new buffer
        free the old buffer

    There are two better algorithms:

    while (!done) {
        grab an amount of text equal to the amount we've grabbed so far
        allocate a new buffer which is twice as large as the last buffer
        copy the old text and the new text together into the new buffer
        free the old buffer


    while (!done) {
        grab 2 KB of text
        add this to the end of a linked list of text chunks

    allocate a buffer whose size is the total size of all the chunks added together
    walk the linked list and copy the text of each chunk into the buffer

    Both "better" algorithms are O(n) but the latter wastes less space.

    Here's the improved code:

    struct Chunk {
        WCHAR text[1024];
        Chunk *next;
        Chunk() : next(nullptr) { text[0] = L'\0'; }

    class DeleteChunksOnExit {
        DeleteChunksOnExit() : m_p(nullptr) {}
        ~DeleteChunksOnExit() {
            Chunk *here = m_p;
            while (here) {
                Chunk *next = here->next;
                delete here;
                here = next;
        void Set(Chunk *p) { m_p = p; }
        Chunk *m_p;


    LPWSTR ReadFromStdIn() {
        Chunk *head = nullptr;
        Chunk *tail = nullptr;

        DeleteChunksOnExit dcoe;
        size_t total_length = 0;
        bool done = false;
        while (!done) {
            Chunk *buffer = new Chunk();
            if (nullptr == buffer) {
                LOG(L"Could not allocate memory for buffer");
                return nullptr;
            if (nullptr == head) {
                // this runs on the first pass only
                head = buffer;
                tail = buffer;
            } else {
                tail->next = buffer;
                tail = buffer;
            if (fgetws(buffer->text, ARRAYSIZE(buffer->text), stdin)) {
                total_length += wcslen(buffer->text);
            } else if (feof(stdin)) {
                done = true;
            } else {
                LOG(L"Error reading from STDIN");
                return nullptr;
        // gather all the allocations into a single string
        size_t size = total_length + 1;
        WCHAR *text = new WCHAR[size];
        if (nullptr == text) {
            LOG(L"Could not allocate memory for text");
            return nullptr;
        DeleteArrayOnExit<WCHAR> deleteText(text);
        WCHAR *temp = text;
        for (Chunk *here = head; here; here = here->next) {
            if (wcscpy_s(temp, size, here->text)) {
                LOG(L"wcscpy_s returned an error");
                return nullptr;
            size_t len = wcslen(here->text);
            temp += len;
            size -= len;
        return text;

Page 1 of 1 (4 items)

October, 2012