Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] regex word

Expand Messages
  • Lim
    ... Yes. It works. Thanks. Regards LIM
    Message 1 of 10 , Apr 22, 2012
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, David Precious <davidp@...> wrote:
      >
      > On Sat, 21 Apr 2012 10:09:33 -0000
      > "Lim" <lim.weijer@...> wrote:
      >
      > > Hi all,
      > >
      > > Is me again.
      > > Can anyone help me to solve a simple question below
      > > I still can not do it correctly.
      > >
      > > for this (4.94588dB,-90.8129?
      > > I wan to save the number of -90.8129 which is just before the "?"
      > > How can I chose it correctly?
      >
      >
      > I think I recall you mentioning this before, but not giving much
      > detail.
      >
      > This should work:
      >
      > /,(.+)\?/
      >
      > DB<1> x "(4.94588dB,-90.8129?" =~ /,(.+)\?/;
      > 0 '-90.8129'
      >
      > This is, of course, assuming that the number you want to extract will
      > always be immediately preceded by a comma and immediately followed by a
      > question mark.
      >
      >
      > -- David Precious ("bigpresh") <davidp@...>
      > http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
      > www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
      > www.preshweb.co.uk/cpan www.preshweb.co.uk/github
      >

      Yes. It works. Thanks.

      Regards
      LIM
    • Lim
      ... Hi, I got a question. If the word I wan to search is not find in a text or sentense. I found that the search will go on without stopping. Is there any idea
      Message 2 of 10 , Apr 22, 2012
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, David Precious <davidp@...> wrote:
        >
        > On Sat, 21 Apr 2012 10:09:33 -0000
        > "Lim" <lim.weijer@...> wrote:
        >
        > > Hi all,
        > >
        > > Is me again.
        > > Can anyone help me to solve a simple question below
        > > I still can not do it correctly.
        > >
        > > for this (4.94588dB,-90.8129?
        > > I wan to save the number of -90.8129 which is just before the "?"
        > > How can I chose it correctly?
        >
        >
        > I think I recall you mentioning this before, but not giving much
        > detail.
        >
        > This should work:
        >
        > /,(.+)\?/
        >
        > DB<1> x "(4.94588dB,-90.8129?" =~ /,(.+)\?/;
        > 0 '-90.8129'
        >
        > This is, of course, assuming that the number you want to extract will
        > always be immediately preceded by a comma and immediately followed by a
        > question mark.
        >
        >
        > -- David Precious ("bigpresh") <davidp@...>
        > http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
        > www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
        > www.preshweb.co.uk/cpan www.preshweb.co.uk/github
        >


        Hi,
        I got a question. If the word I wan to search is not find in a text or sentense. I found that the search will go on without stopping.
        Is there any idea that I can make it stop ?
        I attach my coding at below

        ######
        case.log.txt

        anom: mag(v(out))=(14.6132dB,0°) at 10
        amax: MAX(mag(v(out)))=(14.6133dB,0°) FROM 1 TO 1e+006
        fpk: frequency=(51.4357dB,0°) at 373.064
        amin: MIN(mag(v(out)))=(14.526dB,0°) FROM 1 TO 373.064
        arip: amax/amin=(0.0872961dB,0°)
        frip: frequency=(51.4357dB,0°) at 373.064
        f3db: frequency=(63.6962dB,0°) at 1530.42
        phase: v(out)=(11.5869dB,96.8272°) at 1530.42
        ######


        funcval=case.log

        sub anom
        {
        # gets funcval from file
        # open()
        open (LOGFILE,$funcfile) || die "Can't open source file : $funcfile\n";
        $_=<LOGFILE>;chomp;
        $go=1;
        while($go)
        {
        $_=<LOGFILE>;
        if(m/\banom\b(.*?)=\s*(\S+)/) # this will not stop until find
        {
        $_=$fval=$2;

        if(m/\(([^\,]+)/) # \,([^\)])+/) search for ( .... , to read ($fval_mag_db, $fval_phase_deg) expressions
        {
        $fval_mag=$1; # get ....
        $fvalanom=substr($fval_mag,0,-2); # delete db suffix
        if($fval=='-1.#INF')
        {$fval=0;}
        else
        {$fval=10**($fval/20);} # compute real-value from db
        $funcval=$fval;
        #$_=<LOGFILE>;$_=<LOGFILE>;
        #print "\n$_\n";
        print "\n\nanom\tfrom\tdeck: $fvalanom";
        $go=0;
        }
        }

        }
        close(LOGFILE);
        }

        #######
        in case the "anom" are missing from my case.log text. then the search will continue without stopping.
        how to stop and send an error message?

        Regards,
        LIM
      • Lim
        ... Here I got a big problem regarding to this.I found that my searching will not stop if it does not match any word in the file or text.I attach my example at
        Message 3 of 10 , Apr 22, 2012
        • 0 Attachment
          --- In perl-beginner@yahoogroups.com, "Lim" <lim.weijer@...> wrote:
          >
          > Hi all,
          >
          > Is me again.
          > Can anyone help me to solve a simple question below
          > I still can not do it correctly.
          >
          > for this (4.94588dB,-90.8129?
          > I wan to save the number of -90.8129 which is just before the "?"
          > How can I chose it correctly?
          >
          > Regards,
          > LIM
          >





          Here I got a big problem regarding to this.I found that my searching
          will not stop if it does not match any word in the file or text.I attach
          my example at below
          test.log.txt
          anom: mag(v(out))=(14.6132dB,0°) at 10amax:
          MAX(mag(v(out)))=(14.6133dB,0°) FROM 1 TO 1e+006fpk:
          frequency=(51.4357dB,0°) at 373.064amin:
          MIN(mag(v(out)))=(14.526dB,0°) FROM 1 TO 373.064arip:
          amax/amin=(0.0872961dB,0°)frip: frequency=(51.4357dB,0°) at
          373.064f3db: frequency=(63.6962dB,0°) at 1530.42phase:
          v(out)=(11.5869dB,96.8272°) at 1530.42


          in my perl program
          $funcfile="test.log";&anom
          sub anom{ # gets funcval from file # open() open
          (LOGFILE,$funcfile) || die "Can't open source file : $funcfile\n";
          $_=<LOGFILE>;chomp; $go=1; while($go) { $_=<LOGFILE>;
          if(m/\banom\b(.*?)=\s*(\S+)/) # search for cost ... = val {
          $_=$fval=$2;
          if(m/\(([^\,]+)/) # \,([^\)])+/) search for ( .... , to read
          ($fval_mag_db, $fval_phase_deg) expressions { $fval_mag=$1; # get
          .... $fvalanom=substr($fval_mag,0,-2); # delete db suffix
          if($fval=='-1.#INF') {$fval=0;} else
          {$fval=10**($fval/20);} # compute real-value from db $funcval=$fval;
          #$_=<LOGFILE>;$_=<LOGFILE>; #print "\n$_\n"; print
          "\n\nanom\tfrom\tdeck: $fvalanom"; $go=0; } }
          } close(LOGFILE);}

          when I try to search for the anom in the test.log. it will find out.But
          if the anom are disappear from the text. The program will continue
          without stopping.
          Any people have any idea to help me?
          Regards,LIM


          [Non-text portions of this message have been removed]
        • Brian F. Yulga
          ... Stop there. $go is unnecessary and is tripping you up. Start those lines over with: open ( my $logfile,
          Message 4 of 10 , Apr 22, 2012
          • 0 Attachment
            Lim wrote:
            >
            >
            > --- In perl-beginner@yahoogroups.com
            > <mailto:perl-beginner%40yahoogroups.com>, "Lim" <lim.weijer@...> wrote:
            > >
            > >
            >
            > Here I got a big problem regarding to this.I found that my searching
            > will not stop if it does not match any word in the file or text.I attach
            > my example at below
            >
            > in my perl program
            > $funcfile="test.log";&anom
            > sub anom{ # gets funcval from file # open() open
            > (LOGFILE,$funcfile) || die "Can't open source file : $funcfile\n";
            > $_=<LOGFILE>;chomp; $go=1; while($go) { $_=<LOGFILE>;
            >
            Stop there. $go is unnecessary and is tripping you up. Start those
            lines over with:

            open ( my $logfile, '<', $funcfile ) or die "Can't open source file
            '$funcfile', $!";
            while ( <$logfile> ) {
            chomp;

            Each iteration of the loop sets $_ to the next line of the input file.
            It exits the loop automatically at end of file. If you're only looking
            for the first successful match in your processing, you can exit the loop
            earlier with 'last'.


            > when I try to search for the anom in the test.log. it will find out.But
            > if the anom are disappear from the text. The program will continue
            > without stopping.
            > Any people have any idea to help me?
            > Regards,LIM
            >
            Not surprising; probably $go only gets set to zero if it finds a match.
            Hard to know for sure because your code is not formatted very well.
            Next time, put each statement on one line, and remove commented out
            statements.

            Also, I suggest you reconsider your choice to assign $_ in multiple
            places. You can apply a regex test to any variable:

            if ( $var =~ /regex/ ) {
            # match
            } else {
            # no match
            }

            Hope this helps,
            Brian
          • timothy adigun
            ... Lim --- In perl-beginner@yahoogroups.com, Lim wrote: Lim Lim Hi all, Lim Lim Is me again. Lim Can
            Message 5 of 10 , Apr 22, 2012
            • 0 Attachment
              >>>>> "Lim" == Lim <lim.weijer@...> wrote:
              Lim> --- In perl-beginner@yahoogroups.com, "Lim" <lim.weijer@...>
              wrote:
              Lim> >
              Lim> > Hi all,
              Lim> >
              Lim> > Is me again.
              Lim> > Can anyone help me to solve a simple question below
              Lim> > I still can not do it correctly.
              Lim> >
              Lim> > for this (4.94588dB,-90.8129?
              Lim> > I wan to save the number of -90.8129 which is just before the
              "?"
              Lim> > How can I chose it correctly?
              Lim> >
              Lim> > Regards,
              Lim> > LIM
              Lim> >
              Lim> Here I got a big problem regarding to this.I found that my
              searching
              Lim> will not stop if it does not match any word in the file or text.I
              attach
              Lim> my example at below
              Lim> test.log.txt
              Lim> anom: mag(v(out))=(14.6132dB,0�) at 10amax:
              Lim> MAX(mag(v(out)))=(14.6133dB,0�) FROM 1 TO 1e+006fpk:
              Lim> frequency=(51.4357dB,0�) at 373.064amin:
              Lim> MIN(mag(v(out)))=(14.526dB,0�) FROM 1 TO 373.064arip:
              Lim> amax/amin=(0.0872961dB,0�)frip: frequency=(51.4357dB,0�) at
              Lim> 373.064f3db: frequency=(63.6962dB,0�) at 1530.42phase:
              Lim> v(out)=(11.5869dB,96.8272�) at 1530.42
              Lim> in my perl program

              1. Your perl program was not properly formatted,
              2. It was not clear what you really wanted from your program and what
              your program was intended to achieve.
              I tried some reconstruction though, I hope it point you in the right
              direction. Please check below.

              Lim> $funcfile="test.log";&anom
              don't use '&' to call a subroutine, except on some occasions, just use
              anom().
              Lim> sub anom{ # gets funcval from file # open() open
              Lim> (LOGFILE,$funcfile) || die "Can't open source file : $funcfile\n";

              it better to use 3 arugement open function, see code below.

              Lim> $_=<LOGFILE>;chomp; $go=1; while($go) { $_=<LOGFILE>;
              Lim> if(m/\banom\b(.*?)=\s*(\S+)/) # search for cost ... = val {

              you can test for 'anom' and use regex to get your needed values,
              however, there are some regex you used, that I don't know the need for them
              e.g why? '\s*' after '=', when it is obvious there is no space from the
              test.log file. Or am I checking a wrong file? Though, in my regex below, I
              used it (though, made it optional), in case you fore-see, an occassion for
              such.

              Lim> $_=$fval=$2;
              Lim> if(m/\(([^\,]+)/) # \,([^\)])+/) search for ( .... , to read
              Lim> ($fval_mag_db, $fval_phase_deg) expressions { $fval_mag=$1; # get
              Lim> .... $fvalanom=substr($fval_mag,0,-2); # delete db suffix

              your value $fval,$fvalanom,$fval_mag, can all be obtained using a single
              regex. Though some of these variables were not used in the final print out.
              why?

              Lim> if($fval=='-1.#INF') {$fval=0;} else
              Lim> {$fval=10**($fval/20);} # compute real-value from db
              $funcval=$fval;
              Lim> #$_=<LOGFILE>;$_=<LOGFILE>; #print "\n$_\n"; print
              Lim> "\n\nanom\tfrom\tdeck: $fvalanom"; $go=0; } }
              Lim> } close(LOGFILE);}
              Lim> when I try to search for the anom in the test.log. it will find
              out.But
              Lim> if the anom are disappear from the text. The program will continue
              Lim> without stopping.

              Please, take the script below as a guide in the right direction, it a
              form of reconstruction of what I think you are trying to do.I used the
              test.log file you provided. I can only hope these helps.

              Lim> Any people have any idea to help me?
              Lim> Regards,LIM
              Lim> [Non-text portions of this message have been removed]
              Lim>
              Lim> Reply to sender | Reply to group | Reply via web post | Start a
              New Topic
              Lim> Messages in this topic (3)
              Lim> Recent Activity:
              Lim> New Members 3
              Lim> Visit Your Group
              Lim> Unsubscribing info is here:
              http://help.yahoo.com/help/us/groups/groups-32.html
              Lim> Yahoo! Groups
              Lim> Switch to: Text-Only, Daily Digest � Unsubscribe � Terms of Use

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

              my $func_file = "test.log";
              anom($func_file);

              sub anom {
              my ($file) = @_;
              my $status = 0;
              open my $fh, '<', $file or die "Can't open source file $file:$!";
              while (<$fh>) {
              chomp;
              if (/^anom/) {
              ++$status;
              m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/;
              my $fvalanom = $1;
              my $fval = $2;
              if ( $fval eq '-1.#INF' ) { $fval = 0; }
              else {
              $fval = 10**( $fval / 20 );
              }
              print "\n\nanom\tfrom\tdeck: $fvalanom=$fval";
              }
              }
              close($fh) or die "can't close file:$!";
              print "Error Message: anom not contained in this file\n" if $status ==
              0;
              }


              [Non-text portions of this message have been removed]
            • merlyn@stonehenge.com
              ... timothy m/.+? ((.+?) )=(?: s+)? ((.+?)dB,(.+?) )/; timothy my $fvalanom = $1; timothy my $fval = $2; Please
              Message 6 of 10 , Apr 22, 2012
              • 0 Attachment
                >>>>> "timothy" == timothy adigun <2teezperl@...> writes:

                timothy> m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/;
                timothy> my $fvalanom = $1;
                timothy> my $fval = $2;

                Please don't do this. NEVER use $1 unless you are also checking the
                status of the match. If the match fails, you get a *stale* $1 from a
                *previous* match, making for obscure bugs.

                --
                Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                See http://methodsandmessages.posterous.com/ for Smalltalk discussion
              • timothy adigun
                ... Ran timothy == timothy adigun writes: Ran timothy m/.+? ((.+?) )=(?: s+)? ((.+?)dB,(.+?) )/; Ran timothy
                Message 7 of 10 , Apr 22, 2012
                • 0 Attachment
                  >>>>> "Ran" == Randal L. Schwartz <merlyn@...> wrote:
                  Ran> >>>>> "timothy" == timothy adigun <2teezperl@...> writes:
                  Ran> timothy>
                  m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/;
                  Ran> timothy> my $fvalanom = $1;
                  Ran> timothy> my $fval = $2;

                  It's better written then as:
                  if(m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/){
                  my $fvalanom = $1;
                  my $fval = $2;
                  ...
                  ...
                  }
                  Ran> Please don't do this. NEVER use $1 unless you are also checking
                  the
                  Ran> status of the match. If the match fails, you get a *stale* $1
                  from a
                  Ran> *previous* match, making for obscure bugs.
                  Thanks Randal for that!


                  [Non-text portions of this message have been removed]
                • merlyn@stonehenge.com
                  ... Ran timothy == timothy adigun writes: Ran timothy timothy m/.+? ((.+?) )=(?: s+)? ((.+?)dB,(.+?) )/; Ran timothy
                  Message 8 of 10 , Apr 22, 2012
                  • 0 Attachment
                    >>>>> "timothy" == timothy adigun <2teezperl@...> writes:

                    >>>>>> "Ran" == Randal L. Schwartz <merlyn@...> wrote:
                    Ran> >>>>> "timothy" == timothy adigun <2teezperl@...> writes:
                    Ran> timothy>
                    timothy> m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/;
                    Ran> timothy> my $fvalanom = $1;
                    Ran> timothy> my $fval = $2;

                    timothy> It's better written then as:
                    timothy> if(m/.+?\((.+?)\)=(?:\s+)?\((.+?)dB,(.+?)\)/){
                    timothy> my $fvalanom = $1;
                    timothy> my $fval = $2;
                    timothy> ...
                    timothy> ...
                    timothy> }


                    Or even

                    if (my($fvalanom,$fval) = m{ ... }) { ...
                    }

                    --
                    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                    <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
                  Your message has been successfully submitted and would be delivered to recipients shortly.