Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default qr// question


    How can one know the difference between $arr[0] and $arr[1]?

    eg.
    my @arr = ('^match') x 2;
    $_ = qr/$_/ for $arr[0];

    I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    consuming?)



    --
    Matija
    Matija Papec Guest

  2. Similar Questions and Discussions

    1. Newbie Question: Biz Card Template Question
      Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I...
  3. #2

    Default Re: qr// question

    Matija Papec wrote:
    >
    > How can one know the difference between $arr[0] and $arr[1]?
    Use 'eq' or '==' to compare them?

    > eg.
    > my @arr = ('^match') x 2;
    In this instance the contents of $arr[0] and $arr[1] are exactly the
    same.

    > $_ = qr/$_/ for $arr[0];
    >
    > I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    > consuming?)
    What do you REALLY want to do?


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  4. #3

    Default Re: qr// question

    On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
    wrote:
    >> my @arr = ('^match') x 2;
    >
    >In this instance the contents of $arr[0] and $arr[1] are exactly the
    >same.
    Yes, but after below line they /somehow/ aren't.
    >> $_ = qr/$_/ for $arr[0];
    >>
    >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    >> consuming?)
    >
    >What do you REALLY want to do?
    I want hash/array of regexes but don't want to instantly compile them
    all. I would like to compile them only when some particular is needed
    and that "some particular" may be used more then once for matching,
    while some regexes may not be used at all. If regex compiling is
    significantly time consuming then this matters.

    Matija Papec Guest

  5. #4

    Default Re: qr// question

    On Tue, 09 Sep 2003 08:30:57 +0200, Matija Papec <mpapec@yahoo.com> wrote:
    > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
    > wrote:
    >
    >>> my @arr = ('^match') x 2;
    >>
    >>In this instance the contents of $arr[0] and $arr[1] are exactly the
    >>same.
    >
    > Yes, but after below line they /somehow/ aren't.
    >
    >>> $_ = qr/$_/ for $arr[0];
    >>>
    >>> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    >>> consuming?)
    >>
    >>What do you REALLY want to do?
    >
    > I want hash/array of regexes but don't want to instantly compile them
    > all. I would like to compile them only when some particular is needed
    > and that "some particular" may be used more then once for matching,
    > while some regexes may not be used at all. If regex compiling is
    > significantly time consuming then this matters.
    Something like:

    use Memoize;
    memoize('to_regex');
    sub to_regex {
    return qr/$_[0]/;
    }

    or

    my %regexes;
    sub to_regex {
    $regexes{$_[0]} = qr/$_[0]/ unless exists $regexes{$_[0]};
    return $regexes{$_[0]};
    }

    And then when you want to match use

    $re = to_regex($string_of_regex);

    to get the regex for that string.

    Or with an array (or hash with tr/[]/{}/ ) of regexes you could do:

    $arr[$index] = qr/$arr[$index]/ unless ref $arr[$index];

    Before using $arr[$index] as a regex.

    --
    Sam Holden

    Sam Holden Guest

  6. #5

    Default Re: qr// question

    Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
    > wrote:
    >
    > >> my @arr = ('^match') x 2;
    > >
    > >In this instance the contents of $arr[0] and $arr[1] are exactly the
    > >same.
    >
    > Yes, but after below line they /somehow/ aren't.
    >
    > >> $_ = qr/$_/ for $arr[0];
    > >>
    > >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    > >> consuming?)
    > >
    > >What do you REALLY want to do?
    >
    > I want hash/array of regexes but don't want to instantly compile them
    > all. I would like to compile them only when some particular is needed
    > and that "some particular" may be used more then once for matching,
    > while some regexes may not be used at all. If regex compiling is
    > significantly time consuming then this matters.
    Access the regexes through this routine:

    sub to_regex {
    ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    }

    Strings will successively be turned into regexes on first use.

    Anno
    Anno Siegel Guest

  7. #6

    Default Re: qr// question

    [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) writes:
    > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
    > > wrote:
    > >
    > > >> my @arr = ('^match') x 2;
    > > >
    > > >In this instance the contents of $arr[0] and $arr[1] are exactly the
    > > >same.
    > >
    > > Yes, but after below line they /somehow/ aren't.
    > >
    > > >> $_ = qr/$_/ for $arr[0];
    > > >>
    > > >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    > > >> consuming?)
    > > >
    > > >What do you REALLY want to do?
    > >
    > > I want hash/array of regexes but don't want to instantly compile them
    > > all. I would like to compile them only when some particular is needed
    > > and that "some particular" may be used more then once for matching,
    > > while some regexes may not be used at all. If regex compiling is
    > > significantly time consuming then this matters.
    And if it isn't then it doesn't.
    > Access the regexes through this routine:
    >
    > sub to_regex {
    > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > }
    The text and the subroutine invocation are probably more expensive than the
    qr//.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Benchmark;

    sub to_regex {
    ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    }

    timethese 10 => {
    to_regex => sub {
    my $q = q/x..........[yz]+[[:upper:]]*z(?=r)/;
    $q = to_regex $q for 0..100000;
    },
    qr => sub {
    my $q = q/x..........[yz]+[[:upper:]]*z(?=r)/;
    $q = qr/$q/ for 0..100000;
    },
    };
    __END__

    Benchmark: timing 10 iterations of qr, to_regex...
    qr: 4 wallclock secs ( 3.52 usr + 0.00 sys = 3.52 CPU) @ 2.84/s (n=10)
    to_regex: 6 wallclock secs ( 6.91 usr + 0.00 sys = 6.91 CPU) @ 1.45/s (n=10)


    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

  8. #7

    Default Re: qr// question

    Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
    > [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) writes:
    >
    > > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > > > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
    > > > wrote:
    > > >
    > > > >> my @arr = ('^match') x 2;
    > > > >
    > > > >In this instance the contents of $arr[0] and $arr[1] are exactly the
    > > > >same.
    > > >
    > > > Yes, but after below line they /somehow/ aren't.
    > > >
    > > > >> $_ = qr/$_/ for $arr[0];
    > > > >>
    > > > >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
    > > > >> consuming?)
    > > > >
    > > > >What do you REALLY want to do?
    > > >
    > > > I want hash/array of regexes but don't want to instantly compile them
    > > > all. I would like to compile them only when some particular is needed
    > > > and that "some particular" may be used more then once for matching,
    > > > while some regexes may not be used at all. If regex compiling is
    > > > significantly time consuming then this matters.
    >
    > And if it isn't then it doesn't.
    >
    > > Access the regexes through this routine:
    > >
    > > sub to_regex {
    > > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > > }
    >
    > The text and the subroutine invocation are probably more expensive than the
    > qr//.
    Regex compilation *is* rather expensive. The real reason is that /$re/
    doesn't compile the regex again when $re is a single qr//-quoted value.
    That would explain your benchmarks. It also makes this a rather futile
    exercise.

    [snip benchmark]

    Anno
    Anno Siegel Guest

  9. #8

    Default Re: qr// question

    On 9 Sep 2003, Anno Siegel wrote:
    >Access the regexes through this routine:
    >
    > sub to_regex {
    > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > }
    >
    >Strings will successively be turned into regexes on first use.
    .... and every time after that. = binds more loosely than ?:

    perlmonk:~ $ perl -MO=Deparse,-p -e 'ref $a ? $a : $a = $b'
    ((ref($a) ? $a : $a) = $b);

    It's the same trap as $cond ? $a = $b : $a = $c, which some people use
    instead of $a = ($cond ? $b : $c), but always sets $a to $c.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan Guest

  10. #9

    Default Re: qr// question

    X-Ftn-To: Anno Siegel

    [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:
    >> and that "some particular" may be used more then once for matching,
    >> while some regexes may not be used at all. If regex compiling is
    >> significantly time consuming then this matters.
    >
    >Access the regexes through this routine:
    >
    > sub to_regex {
    > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > }
    >
    >Strings will successively be turned into regexes on first use.
    Tnx, so ref knows all about them! :) Unfortunately perldoc isn't so
    informative. :!


    --
    Matija
    Matija Papec Guest

  11. #10

    Default Re: qr// question

    Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote in comp.lang.perl.misc:
    > On 9 Sep 2003, Anno Siegel wrote:
    >
    > >Access the regexes through this routine:
    > >
    > > sub to_regex {
    > > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > > }
    > >
    > >Strings will successively be turned into regexes on first use.
    >
    > ... and every time after that. = binds more loosely than ?:
    Oh, right. Thanks for catching that. Parentheses around the assignment
    would fix it, but, as noted in another branch of the thread, it doesn't
    save on regex compilation either way: qr// (well, the regex compiler itself)
    already does that.

    Anno
    Anno Siegel Guest

  12. #11

    Default Re: qr// question

    Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > X-Ftn-To: Anno Siegel
    >
    > [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:
    > >> and that "some particular" may be used more then once for matching,
    > >> while some regexes may not be used at all. If regex compiling is
    > >> significantly time consuming then this matters.
    > >
    > >Access the regexes through this routine:
    > >
    > > sub to_regex {
    > > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
    > > }
    > >
    > >Strings will successively be turned into regexes on first use.
    >
    > Tnx, so ref knows all about them! :) Unfortunately perldoc isn't so
    > informative. :!
    You can think of qr/.../ values as objects of class Regexp, which has
    stringification overloaded. That describes most of their behavior, though
    things aren't really that simple.

    perl -Moverload -le'print "yup" if overload::Overloaded( qr//)'

    prints nothing.

    Anno
    Anno Siegel Guest

  13. #12

    Default Re: qr// question

    X-Ftn-To: Sam Holden

    [email]sholden@flexal.cs.usyd.edu.au[/email] (Sam Holden) wrote:
    >my %regexes;
    >sub to_regex {
    > $regexes{$_[0]} = qr/$_[0]/ unless exists $regexes{$_[0]};
    > return $regexes{$_[0]};
    >}
    >
    >And then when you want to match use
    >
    >$re = to_regex($string_of_regex);
    >
    >to get the regex for that string.
    >
    >Or with an array (or hash with tr/[]/{}/ ) of regexes you could do:
    >
    >$arr[$index] = qr/$arr[$index]/ unless ref $arr[$index];
    >
    >Before using $arr[$index] as a regex.
    Tnx, last one looks most promising as it doesn't use any additional
    variables.



    --
    Matija
    Matija Papec Guest

  14. #13

    Default Re: qr// question

    Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:
    [qr// values]
    > >That describes most of their behavior, though
    > >things aren't really that simple.
    > >
    > > perl -Moverload -le'print "yup" if overload::Overloaded( qr//)'
    > >
    > >prints nothing.
    >
    > Obviously consistency is hard thing to achieve.
    "Perl does what you want, unless you want consistency"

    Anno
    Anno Siegel Guest

  15. #14

    Default Re: qr// question

    Anno Siegel wrote:
    >
    > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > >
    > > Obviously consistency is hard thing to achieve.
    >
    > "Perl does what you want, unless you want consistency"
    Can I quote you on that? :-)


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  16. #15

    Default Re: qr// question

    John W. Krahn <krahnj@acm.org> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > >
    > > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
    > > >
    > > > Obviously consistency is hard thing to achieve.
    > >
    > > "Perl does what you want, unless you want consistency"
    >
    > Can I quote you on that? :-)
    You'd be quoting Larry Wall, more or less, or at least some edition of
    the Camel. It wasn't about Perl in general, but, I think it assignment
    in various contexts that he said that.

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139