Help with variable with regex inside.

Discussion in 'Perl Misc' started by timb, Dec 4, 2006.

  1. timb

    timb Guest

    Hello.
    I am writing a perl program that takes in a config file with different
    regular expressions and parameters to parse dates. The date is in the
    format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
    split it into the $month, $day, and $year


    Config file:

    TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
    DateRegex = $2/$3/$1


    Config loading method:

    open (CONFIG, "configfile.config") ||
    die "Couldn't load config file for some reason";

    while (<CONFIG>) {
    chomp; # no newline
    s/#.*//; # no comments
    s/^\s+//; # no leading white
    s/\s+$//; # no trailing white
    next unless length; # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    no strict 'refs';
    $$var = $value;
    }
    close(CONFIG);


    Date parsing subroutine:

    sub getdate {
    $rsstitle =~ m/$RssTitleRegex/ig;
    eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
    die if $@;
    ($month, $day, $year) = split(/\//, $DateRegex);
    }
     
    timb, Dec 4, 2006
    #1
    1. Advertising

  2. timb

    tim Guest

    I forgot to add the out put of $DateRegex:
    //$1
    -Tim

    timb wrote:
    > Hello.
    > I am writing a perl program that takes in a config file with different
    > regular expressions and parameters to parse dates. The date is in the
    > format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
    > split it into the $month, $day, and $year
    >
    >
    > Config file:
    >
    > TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
    > DateRegex = $2/$3/$1
    >
    >
    > Config loading method:
    >
    > open (CONFIG, "configfile.config") ||
    > die "Couldn't load config file for some reason";
    >
    > while (<CONFIG>) {
    > chomp; # no newline
    > s/#.*//; # no comments
    > s/^\s+//; # no leading white
    > s/\s+$//; # no trailing white
    > next unless length; # anything left?
    > my ($var, $value) = split(/\s*=\s*/, $_, 2);
    > no strict 'refs';
    > $$var = $value;
    > }
    > close(CONFIG);
    >
    >
    > Date parsing subroutine:
    >
    > sub getdate {
    > $rsstitle =~ m/$RssTitleRegex/ig;
    > eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
    > die if $@;
    > ($month, $day, $year) = split(/\//, $DateRegex);
    > }
     
    tim, Dec 4, 2006
    #2
    1. Advertising

  3. On Sun, 03 Dec 2006 23:05:31 -0800, timb wrote:

    > Hello.
    > I am writing a perl program that takes in a config file with different
    > regular expressions and parameters to parse dates. The date is in the
    > format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
    > split it into the $month, $day, and $year


    You could split it into ($day, $month, $year). The position of a named
    variable is irrelevant.

    >
    >
    > Config file:
    >
    > TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
    > DateRegex = $2/$3/$1


    That is not a regex, it is a substitution pattern.

    >
    >
    > Config loading method:
    >
    > open (CONFIG, "configfile.config") ||
    > die "Couldn't load config file for some reason";


    die "Couldn't load config file: $!";

    >
    > while (<CONFIG>) {
    > chomp; # no newline
    > s/#.*//; # no comments
    > s/^\s+//; # no leading white
    > s/\s+$//; # no trailing white
    > next unless length; # anything left?
    > my ($var, $value) = split(/\s*=\s*/, $_, 2);
    > no strict 'refs';
    > $$var = $value;
    > }
    > close(CONFIG);
    >
    >
    > Date parsing subroutine:
    >
    > sub getdate {
    > $rsstitle =~ m/$RssTitleRegex/ig;


    Did you mean TitleRegex? And what is $rsstitle?

    > eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
    > die if $@;
    > ($month, $day, $year) = split(/\//, $DateRegex);
    > }


    It is completely unclear what you want to accomplish, but it looks like
    you are going about in a horribly complicated way. Wouldn't

    sub getdate {
    ($day, $month, $year) = split(/\//, $rsstitle);
    return ($month, $day, $year);
    }

    also accomplish what you want?

    M4
    --
    Redundancy is a great way to introduce more single points of failure.
     
    Martijn Lievaart, Dec 4, 2006
    #3
  4. timb

    tim Guest

    Sorry for making it more complicated than is should be. The code I
    posted are just the relivant snippets. The problem with your solution:

    > Wouldn't
    >
    > sub getdate {
    > ($day, $month, $year) = split(/\//, $rsstitle);
    > return ($month, $day, $year);
    > }
    >
    > also accomplish what you want?
    >
    > M4


    is that I want to be able to use a variable to determine the input
    format of the date.
    -Tim

    Martijn Lievaart wrote:
    > On Sun, 03 Dec 2006 23:05:31 -0800, timb wrote:
    >
    > > Hello.
    > > I am writing a perl program that takes in a config file with different
    > > regular expressions and parameters to parse dates. The date is in the
    > > format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
    > > split it into the $month, $day, and $year

    >
    > You could split it into ($day, $month, $year). The position of a named
    > variable is irrelevant.
    >
    > >
    > >
    > > Config file:
    > >
    > > TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
    > > DateRegex = $2/$3/$1

    >
    > That is not a regex, it is a substitution pattern.
    >
    > >
    > >
    > > Config loading method:
    > >
    > > open (CONFIG, "configfile.config") ||
    > > die "Couldn't load config file for some reason";

    >
    > die "Couldn't load config file: $!";
    >
    > >
    > > while (<CONFIG>) {
    > > chomp; # no newline
    > > s/#.*//; # no comments
    > > s/^\s+//; # no leading white
    > > s/\s+$//; # no trailing white
    > > next unless length; # anything left?
    > > my ($var, $value) = split(/\s*=\s*/, $_, 2);
    > > no strict 'refs';
    > > $$var = $value;
    > > }
    > > close(CONFIG);
    > >
    > >
    > > Date parsing subroutine:
    > >
    > > sub getdate {
    > > $rsstitle =~ m/$RssTitleRegex/ig;

    >
    > Did you mean TitleRegex? And what is $rsstitle?
    >
    > > eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
    > > die if $@;
    > > ($month, $day, $year) = split(/\//, $DateRegex);
    > > }

    >
    > It is completely unclear what you want to accomplish, but it looks like
    > you are going about in a horribly complicated way. Wouldn't
    >
    > sub getdate {
    > ($day, $month, $year) = split(/\//, $rsstitle);
    > return ($month, $day, $year);
    > }
    >
    > also accomplish what you want?
    >
    > M4
    > --
    > Redundancy is a great way to introduce more single points of failure.
     
    tim, Dec 4, 2006
    #4
  5. On Mon, 04 Dec 2006 00:05:11 -0800, tim wrote:

    > Sorry for making it more complicated than is should be. The code I
    > posted are just the relivant snippets. The problem with your solution:
    >
    >> Wouldn't
    >>
    >> sub getdate {
    >> ($day, $month, $year) = split(/\//, $rsstitle);
    >> return ($month, $day, $year);
    >> }
    >>
    >> also accomplish what you want?
    >>
    >> M4

    >
    > is that I want to be able to use a variable to determine the input
    > format of the date.


    Aha! In that case I would make the input like:

    dateformat = %M/%D/%Y

    and process that, but it would take some writing to accomplish that.

    Now back to your problem. In your code, you do two evals. I think you
    should try with one. Either use /ee on the substitution or use an eval. I
    would go for the latter. Or even none at all.

    (untested)

    DateRegex = ^(\d{4})\/(\d{2})\/(\d{2})$
    DateFormat = %Y/%M/%D

    sub getdate {
    my $date = shift; # give the date as a parameter
    my ($year, $month, $day);
    $date =~ m/$DateRegEx/;
    my @t = ($1, $2, $3);
    for (split(/\//, $DateFormat) {
    /%D/ and do { $day = shift @t; next };
    /%M/ and do { $month = shift @t; next };
    /%Y/ and do { $year = shift @t; next; };
    die "Bad format: $DateFormat";
    }
    return ($month, $day, $year);
    }

    This probably can be done shorter and more elegantly. But this should work.

    HTH,
    M4
    --
    Redundancy is a great way to introduce more single points of failure.
     
    Martijn Lievaart, Dec 4, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    814
  2. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
  3. Dave
    Replies:
    0
    Views:
    439
  4. Er Galv?o Abbott

    Variable inside RegEx

    Er Galv?o Abbott, Feb 16, 2004, in forum: Javascript
    Replies:
    4
    Views:
    98
    Michael Winter
    Feb 16, 2004
  5. Mehmet Yavuz S. Soyturk

    variable declaration inside eval inside with

    Mehmet Yavuz S. Soyturk, Apr 11, 2007, in forum: Javascript
    Replies:
    3
    Views:
    130
    Mehmet Yavuz S. Soyturk
    Apr 12, 2007
Loading...

Share This Page