![]() Sort # no DateTime objects here!ĭateTime->new( year => $y, month => $m, day => $d )->ymd(''),Īlthough you still have a speed penalty for using DateTime, it’s not as bad as it was. If you wanted to do that with DateTime, your program looks almost the same, and at the program level that looks like the same level of complexity, since the structure doesn’t change that much: # sort_dates_datetime If all you want to do is put those lines in order, you can split each line, split each date field, and use the reconstituted YYYYMMDD date in a Schwartzian Transform: # sort_dates_sprintf There are multiple ways we can compare whether strings are equal or not. This flag will tell Perl to remember where it was in the string when it returns to it. You can avoid this by using the g flag with your regex. NOTE: String::Approx suits the task of string matching, not string comparison, and it works for strings. Perl always begins matching at the beginning of the string, so it will always find the 200, and never get to the following numbers. With this you can emulate errors: typing errorrs, speling errors, closely related vocabularies (colour color), genetic mutations (GAG ACT), abbreviations (McScot, MacScot). % perl datemaker 100000 > dates100000.txt String::Approx lets you match and substitute strings approximately. That sounds like odd advice when most of Effective Perl Programming is about using the right module, but as you become an experienced and Effective Perler, it’s up to you to judiciously apply any advice you get, including whether or not you should use modules.Ĭonsider a format where you have some input data that contains a date on each line: some line of dataĬreate a script to make some sample data: # datemakerĬreate some files of various sizes: % perl datemaker 10 > dates10.txt If you don’t need to use the dates in your input for anything other than ordering (for example, you don’t need to compute durations or align the same week of different years), you might consider ditching the date modules altogether. You’ve already seen this in the various techniques that you can use to sort a list in Item 22: Learn the myriad ways of sorting. Compare strings in lexicographical order. Perl string comparison operator Perl string comparison operator. Sometimes the overhead of objects, which have to call (perhaps many) subroutines to do their work, is too expensive. Perl has the keywords eq, ne, gt, ge, lt, le as string comparison operators, which are derived from the numerical comparison operators in shell scripts (sh, bash, csh, zsh, ksh). In your heady rush for program purity and elegance, don’t think that you always have to use objects to do your work. There are many excellent date and time modules on CPAN, including most people’s favorite, DateTime. Using these variables is not recommended in scripts when performance. Just because you find a module that does something doesn’t mean that you have to use it. (dollar backtick) holds the part of the string before (to the left of) the regex match. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |