3dbc has asked for the wisdom of the Perl Monks concerning the following question:

Dear Almighty Perl Mongers,

My thought was to convert this mess to a regex substitution.

$bday = "8/16/97"; $firstname = "Benjamin"; $lastname = "Biederman"; @DOB = split /\//, $bday; @month = split //, $DOB[0]; @day = split //, $DOB[1]; $digits_month = @month; $digits_day = @day; $temp_month = $DOB[0]; $temp_day = $DOB[1]; if ($digits_month==1) { $bday = "0"; $bday .= $temp_month; } else { $bday = $temp_month; } if ($digits_day==1) { $bday .= "0"; $bday .= $temp_day; } else { $bday .= $temp_day; } @new_username_a = split //, $firstname; @new_username_b = split //, $lastname; $login_name = lc $new_username_a[0]; $login_name .= lc $new_username_b[0]; $login_name .= $bday;
to something like ---
$login_name = "$firstname $lastname $bday"; $login_name = s/^(^\C)\s(^\C)\s(^\d{1,2})\/(\d{1,2})\/(\d{1,2})/$1$2$3 +$4/; #notice that to 2 digit bit is not reflected by this regex
Thanks,
Chris

janitored by ybiC: Balanced <code> tags around code only, retitle from " REGEX optimization" for better searching

Replies are listed 'Best First'.
Re: Convert split to regex
by holo (Monk) on Dec 08, 2003 at 20:06 UTC

    Shorter code does not necessarily mean faster code. If you need to compose a username from first, last names and date of birth, you can have:

    use strict; my $bday = "8/16/97"; my $firstname = "Benjamin"; my $lastname = "Biederman"; my $uname = lc(substr($firstname,0,1) . substr($lastname,0,1)) . sprintf("%02d"x3, split m|/|, $bday); print $uname;

    Is that what you want ?

Re: Convert split to regex
by Zed_Lopez (Chaplain) on Dec 08, 2003 at 20:07 UTC

    I'd be more inclined toward:

    $login_name = sprintf "%.1s%.1s%02d%02d%02d", lcfirst $first_name, lcf +irst $last_name, split /\//, $bday;

      OK, Zed_Lopez was a lot faster to reply, but what I came up with, on the same lines, is a bit shorter :-), FWIW:

      my $login_name = lc sprintf '%.1s%.1s%02d%02d%02d', $firstname, $lastname, split '/', $bday;

      dave

Re: Convert split to regex
by Aristotle (Chancellor) on Dec 09, 2003 at 13:18 UTC
    @month = split //, $DOB[0]; $digits_month = @month;
    What a convoluted way to say
    $digits_month = length $DOB[0];
    There's many more albeit small examples of pretzel logic in that piece of code. A first cleanup yields
    my $bday = "8/16/97"; my $firstname = "Benjamin"; my $lastname = "Biederman"; my ($month, $day, $year) = split /\//, $bday; my $formatted_bday = ''; if (length($month) == 1) { $formatted_bday = "0"; } $formatted_bday .= $month; if (length($day) == 1) { $formatted_bday .= "0"; } $formatted_bday .= $day; my $login_name = lc substr $firstname, 0, 1; . lc substr $lastname, 0, 1; . $formatted_bday;

    Of course that's still far too elaborate. Builtin functions to do such formatting already exist. Assume that Perl has what you need when you're trying to solve such trivial problems.

    You're also well advised to read Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com.

    Makeshifts last the longest.