Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Grabbing year and month from filename

by htmanning (Pilgrim)
on Aug 01, 2018 at 04:26 UTC ( #1219617=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I have a database full of filenames based on article titles. For example:

"This is an Article Title" would match the filename 2010-7-this-is-an.html.

The old script created the filename with the first 3 words of the title, but it inserted the year and month the article was written at the beginning. It's always in the same format (year-month-filename.html).

I am trying to build filenames based on the full title, but keep the year and month at the beginning.

I can loop through and create new filenames based on the full title, but I don't know how to grab the year and month from the beginning of the filename. Help?

Thanks.

Replies are listed 'Best First'.
Re: Grabbing year and month from filename
by vinoth.ree (Monsignor) on Aug 01, 2018 at 05:26 UTC

    Use regular expression and parse the year and month from the file name.

    $filename =~ /^(\d+)-(\d+)-(.*)/; my $year = $1; my $mon = $2; print("$year-$mon");
    (OR)

    Use split() method

    my $filename = "2010-7-this-is-an.html"; my ($year, $mon) = split('-', $filename); print("$year-$mon");

    All is well. I learn by answering your questions...
Re: Grabbing year and month from filename
by Marshall (Abbot) on Aug 03, 2018 at 03:46 UTC
    I don't want to confuse you, but since you are modifying the filenames, I would suggest an additional enhancement:
    • Make the month number two digits eg, (2010-07) instead of (2010-7)
    The reason for this is so that the directory listing sort order (alpha-numeric) will be the same as a more advanced sort order based upon the specific numeric Year and Month values.
    Right now, your directory listing will come in this order:
    2010-12-another-file.html 2010-7-this-is-an.html
    I think you will like this better:
    2010-07-this-is-an.html 2010-12-another-file.html
    Of course with Perl, there are always a number of ways to do this.
    Here are 2 ways that work well:
    $month =~ s/^(\d)$/0$1/; # adds leading zero if there is just a single # digit- otherwise has no effect # *** I recommend this idea for # your application. **** $month = sprintf("%02d", $month); # re-assigns a new string value # to $month no matter what
    Other ideas occured to me, but I believe that these are the two best alternatives.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1219617]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-07-07 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?