Date in CSV/TSV question - Page 2

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: Date in CSV/TSV question

  BM> There's no need to muck about with the #! line and BEGIN blocks, both of
  BM> which would make it impossible to turn this into a subroutine later:

  BM>     my %months = ...;

  BM>     local $^I = ".bak";
  BM>     while (<>) { ... }

  BM> The edit-in-place handling, including renaming the old file and opening
  BM> and selecting ARGVOUT, is done by the no-filehandle <> operator (or an
  BM> explicit <ARGV> or readline(ARGV)) whenever $^I is set. If you want to
  BM> in-place edit a custom list of files, you can also localise @ARGV.

and File::Slurp has edit_file and edit_file_lines which are even easier
to use.

i do need to add a backup file option to those.


Re: Date in CSV/TSV question

Quoted text here. Click to load it


Quoted text here. Click to load it

The 'BEGIN' serves no useful purpose here: %months needs to be
initialized before the while-loop uses it. Since statements in a file
are executed consecutively (anything else would probably be 'a little
confusing' :-), this will be the case with either variant.

As I wrote in another posting: If perl hadn't been told to destroy the
input file, also telling it to make a backup of that before doing so
wasn't necessary. While this probably doesn't matter much for a
trivial example like this, 'not using -i' also means that the code can
be debugged and fixed without constantly renaming files or losing the
original input file altogether in case the 'backup request' was
accidentally forgotten. This also enables use of the script(let) as
'another filter' in a more complicated pipeline.

Re: Date in CSV/TSV question

Quoted text here. Click to load it

You missed the -p. Ben's code is equivalent to

    $^I = ".bak";
    while (<>) {
        BEGIN {
            %months = ...;
    continue { print }

which is certainly a rather confusing way to write that loop, but does
require the BEGIN to work properly since with -p there is no other way
to move code outside the while loop.

(This is where BEGIN came from originally: perl -p will treat BEGIN and
END the same way awk does. The later expansion into a general 'run code
at compile time' feature was something of an accident.)

(a different Ben)

Re: Date in CSV/TSV question

On Tuesday, January 1, 2013 6:56:14 PM UTC-5, Dr Eberhard Lisse wrote:
Quoted text here. Click to load it

For each line in the file, do something like this, assuming that $date contains
a string that matches the date you want to change:
1. my ($day, $month, $year) = split(/ /, $date);
2. $date = sprintf("%04d-%02d-%02d", $year, $mo2num, $day);

Line 1 splits your date string into the three components: day, month, year.
Line 2 reassembles those three components and assigns the result back to $date.
The hash table %mo2num looks like this:
my %mo2num = (
  JAN => 1,
  FEB => 2,
  mar => 3,


Re: Date in CSV/TSV question



on 2013-01-08 18:35 ccc31807 said the following:
Quoted text here. Click to load it

Re: Date in CSV/TSV question

Quoted text here. Click to load it

And assuming the hash exists (I posted a command generating it two
times), the format can be transformed with a subsitution expression (I
also posted two times), namely


Site Timeline