[31345] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2590 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 8 21:09:43 2009

Date: Tue, 8 Sep 2009 18:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 8 Sep 2009     Volume: 11 Number: 2590

Today's topics:
    Re: add a 3 lines of text in a file before "package" in <tadmc@seesig.invalid>
    Re: add a 3 lines of text in a file before "package" in <uri@StemSystems.com>
    Re: add a 3 lines of text in a file before "package" in <someone@example.com>
    Re: add a 3 lines of text in a file before "package" in <ben@morrow.me.uk>
        FAQ 2.5 I grabbed the sources and tried to compile but  <brian@theperlreview.com>
    Re: IPC::Open2::open2() -- How to pass strings stdin, s <jerry@ieee.org>
    Re: IPC::Open2::open2() -- How to pass strings stdin, s <ben@morrow.me.uk>
    Re: Need expert help matching a line <ramon@conexus.net>
    Re: Need expert help matching a line <ramon@conexus.net>
    Re: Need expert help matching a line sln@netherlands.com
    Re: Need expert help matching a line <ramon@conexus.net>
    Re: Perl regex expression to return values sln@netherlands.com
    Re: Perl regex expression to return values <someone@example.com>
    Re: replace words sln@netherlands.com
    Re: XML::Parser sln@netherlands.com
    Re: XML::Parser <tadmc@seesig.invalid>
    Re: XML::Parser <yankeeinexile@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 08 Sep 2009 13:36:15 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: add a 3 lines of text in a file before "package" in file
Message-Id: <slrnhad8bc.16l.tadmc@tadmc30.sbcglobal.net>

mike <mikaelpetterson@hotmail.com> wrote:
> Hi,
>
> I am using perl and need to add some text lines before the word
> "package" in the file. I have read about Tie::File
> and I am trying to use it as below. But I am unable to see how I can
> match the word "package" and add
> /* ---- Insert text here ---- */
>  on  the line before "package".
>
> Any ideas?
>
> //mike
>
> sub modify {
> 	my @line_array;
> 	my $line;
> 	my ($file) = @_;
> 	print "Modifying file, $file\n";
> 	tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";


foreach my $i ( 0 .. $#line_array) {
    if ($line_array[$i] =~ /package/) {
        $line_array[$i] =  "/* ---- Insert text here ---- */\n$line_array[$i]"
        last;
    }
}


> 	untie @line_array;
>
> }


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Tue, 08 Sep 2009 14:48:42 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: add a 3 lines of text in a file before "package" in file
Message-Id: <87ljkpgtmd.fsf@quad.sysarch.com>

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth mike <mikaelpetterson@hotmail.com>:
  >> 
  >> I am using perl and need to add some text lines before the word
  >> "package" in the file. I have read about Tie::File
  >> and I am trying to use it as below. But I am unable to see how I can
  >> match the word "package" and add
  >> /* ---- Insert text here ---- */
  >> on  the line before "package".

  BM> Is the file likely to be large (which probably means several hundred MB,
  BM> nowadays)? If not, it would be easier to use File::Slurp and do a simple
  BM> s/// on the whole file before writing it out again.


yes, slurping is fine for many megabytes today which is something people
have to learn. text files have not grown like ram sizes have (other than
biogen and similar stuff). slurping this is easy and fast:

(untested)

use File::Slurp ;

my $text = read_file( $file ) ;
$text =~ s{(^.*package.*$)}
	  {/* ---- Insert text here ---- */\n$1}m ;
write_file( $file, $text ) ;

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Tue, 08 Sep 2009 12:20:34 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: add a 3 lines of text in a file before "package" in file
Message-Id: <28ypm.157221$cf6.47750@newsfe16.iad>

mike wrote:
> 
> I am using perl and need to add some text lines before the word
> "package" in the file.

$ echo "one
two
three
four package
five
six" | perl -pe'/package/&&print "new text 1\nnew text 2\nnew text 2\n"'
one
two
three
new text 1
new text 2
new text 2
four package
five
six



John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


------------------------------

Date: Tue, 8 Sep 2009 21:38:30 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: add a 3 lines of text in a file before "package" in file
Message-Id: <6vrin6-qil1.ln1@osiris.mauzo.dyndns.org>


Quoth Tad J McClellan <tadmc@seesig.invalid>:
> mike <mikaelpetterson@hotmail.com> wrote:
> >
> > sub modify {
> > 	my @line_array;
> > 	my $line;
> > 	my ($file) = @_;
> > 	print "Modifying file, $file\n";
> > 	tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";
> 
> 
> foreach my $i ( 0 .. $#line_array) {

This will read the entire file to count the lines before it starts,
removing any benefit of using Tie::File.

It occurs to me that the correct answer is

    perl -lpe'/package/ and print "/*...*/"'

or the written-out equivalent.

Ben



------------------------------

Date: Tue, 08 Sep 2009 22:00:05 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 2.5 I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed.  How do I make it work?
Message-Id: <FtApm.157246$cf6.133183@newsfe16.iad>

This is an excerpt from the latest version perlfaq2.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

2.5: I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed.  How do I make it work?

    Read the INSTALL file, which is part of the source distribution. It
    describes in detail how to cope with most idiosyncrasies that the
    Configure script can't work around for any given system or architecture.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


------------------------------

Date: Tue, 8 Sep 2009 12:09:19 -0700 (PDT)
From: Jerry Krinock <jerry@ieee.org>
Subject: Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
Message-Id: <f0969f5d-c283-42ac-a921-2364eef36486@x25g2000prf.googlegroups.com>

Someone named =E6=9D=8E=E5=B0=8F=E4=BC=9F sent me an even easier way.  Hard=
ly even worth a sub
now, but just so I don't ever forget this....

=3Dcom
Execute a given external program (command).  The program must
take input from stdin.  The program's stdout is returned.

Provide the command, with any command-line options, as the
first parameter, and the input data as the second parameter.

Use this in lieu of open2() and the methods of Expect.pm, both
of which will hang indefinitely unless the program supports
unbuffered I/O, typically a -u option.
=3Dcut
sub getStdoutWithStdinFromCmd {
	my $cmd =3D shift ;
	my $stdin =3D shift ;

	return `echo \"$stdin\" | \"$cmd\"` ;

=3Dcom

Hand hits forehead!!!!  Duh!!!!!!!!


------------------------------

Date: Tue, 8 Sep 2009 21:47:22 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: IPC::Open2::open2() -- How to pass strings stdin, stdout?
Message-Id: <qfsin6-qil1.ln1@osiris.mauzo.dyndns.org>


Quoth Jerry Krinock <jerry@ieee.org>:
> 
> =com

=com is not a pod directive. If you just want un-sectioned pod, use
=pod; if you want a block comment, use

    =begin comment

    ...

    =end comment

    =cut

> Use this in lieu of open2() and the methods of Expect.pm, both
> of which will hang indefinitely unless the program supports
> unbuffered I/O, typically a -u option.

This comment is inaccurate. It is perfectly possible to drive a program
that uses buffered IO using open2, you just need to be ready to feed
input as needed and get output as it arrives.

> sub getStdoutWithStdinFromCmd {
> 	my $cmd = shift ;
> 	my $stdin = shift ;
> 
> 	return `echo \"$stdin\" | \"$cmd\"` ;

This is a bad idea. For one thing, if $stdin contains a '$' you will not
get the input you expect; for another, if $stding is longer than the
(system-specific) maximum command-line length, this will fail.

Forking a child to feed the input pipe might, however, be a sensible
solution. Something like

    use POSIX qw/_exit/;

    my $kid = fork;
    defined $kid or die "can't fork: $!";

    unless ($kid) {
        print *Writer $stdin;
        close *Writer;
        _exit 0;
    }

    close *Writer;
    my $stdout = <Reader>;
    waitpid $kid, 0;

Ben



------------------------------

Date: Tue, 8 Sep 2009 15:20:04 -0700 (PDT)
From: Ramon F Herrera <ramon@conexus.net>
Subject: Re: Need expert help matching a line
Message-Id: <ca8e1f9c-f7e3-4fe1-9d11-31aad59bcccb@k39g2000yqe.googlegroups.com>

On Sep 8, 1:35=A0pm, s...@netherlands.com wrote:
> On Tue, 8 Sep 2009 05:23:32 -0700 (PDT), Ramon F Herrera <ra...@conexus.n=
et> wrote:
>
>
>
>
>
> >This is really a parsing question, but I figure that nobody knows more
> >about regex and pattern matching than Perl programmers.
>
> >I have many files which contain multiple lines of variable-value pair
> >assignments. I need to break down each lines into its 3 constituent
> >components.
>
> >Variable Name =3D Variable Value
>
> >IOW, each line contains 3 parts:
>
> >VariableName
> >Equal Sign
> >VariableValue
>
> >As opposed to the variable names used by many programming languages,
> >my variable names accept embedded space.
>
> >Here's some examples of the lines I am trying to match:
>
> >My Favorite Baseball Player =3D George Herman "Babe" Ruth
> >What did your do on Christmas =3D I rested, computed the % mortgage and
> >visited my brother + sister.
> >Favorite Curse =3D That umpire is a #&*%!
>
> >What I need is a way to specify valid characters.
>
> >VariableName: Alphanumeric (and perhaps underscore), blank space.
> >VariableValue: Pretty much anything is valid on the RHS except an '=3D'
> >sign (I guess)
>
> >Thanks for your kind assistance.
>
> >-Ramon
>
> -sln
>
> use strict;
> use warnings;
>
> my $buf =A0=3D '';
>
> while (<DATA>)
> {
> =A0 =A0 =A0 =A0 if (/=3D/ or eof) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ($buf =3D~ /\s*([\w ]+)\s*=3D\s*((?:.+=
(?:\n .+)*)|)/)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 my ($var,$val) =3D ($1,$2=
);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $val =3D~ s/\n +/\n/g;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "$var =3D> $val\n\n=
";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $buf =3D '';
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 $buf .=3D $_; =A0 =A0}
>
> __DATA__
>
> My Favorite Baseball Player =3D George Herman =3D =A0"Babe" Ruth
> What did your do on Christmas =3D I rested, computed the % mortgage and
> =A0visited my brother + sister.
> =A0asdfasdf=3D
> Favorite Curse =3D That umpire is a #&*%!
> errnngsf
> sngdnsdg
> Describe your summer vacation =3D Well, we traveled to the beach
> =A0 and to the mountains, and debated whether we
> =A0 should go to the Grand Canyon and Niagara falls.
> =A0 The GPS you gave me turned out to be very useful!


Thank you, sln!

I have to clarify that my program is not written in Perl (language
that I haven't used in ages) but in C++. The reason I posted my
question in this NG will be understood by reading this:

http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/syntax=
 .html

I am sticking with the default (Perl) Regex syntax.

This is the relevant code that I have so far. As you can see it is
rather simplistic. I am not implementing continuation lines yet.

const string variable =3D "([\\w ]+)";
const char equal_sign =3D '=3D';
const string value    =3D "([\\w ]+)";

const string assignment =3D variable + equal_sign + value;

The question that I have is this: how do I restrict the LHS to begin
with an alphabetic characters? IOW: The LHS may contain blanks but
they cannot be the first character of the line. I will also be
accepting digits, periods and underscores on the LHS but again, the
variable name cannot begin with any of them.

TIA,

-Ramon



------------------------------

Date: Tue, 8 Sep 2009 15:38:21 -0700 (PDT)
From: Ramon F Herrera <ramon@conexus.net>
Subject: Re: Need expert help matching a line
Message-Id: <42559ae9-0142-4595-b82f-6bd5586548fc@w10g2000yqf.googlegroups.com>

On Sep 8, 6:20=A0pm, Ramon F Herrera <ra...@conexus.net> wrote:
> On Sep 8, 1:35=A0pm, s...@netherlands.com wrote:
>
>
>
> > On Tue, 8 Sep 2009 05:23:32 -0700 (PDT), Ramon F Herrera <ra...@conexus=
 .net> wrote:
>
> > >This is really a parsing question, but I figure that nobody knows more
> > >about regex and pattern matching than Perl programmers.
>
> > >I have many files which contain multiple lines of variable-value pair
> > >assignments. I need to break down each lines into its 3 constituent
> > >components.
>
> > >Variable Name =3D Variable Value
>
> > >IOW, each line contains 3 parts:
>
> > >VariableName
> > >Equal Sign
> > >VariableValue
>
> > >As opposed to the variable names used by many programming languages,
> > >my variable names accept embedded space.
>
> > >Here's some examples of the lines I am trying to match:
>
> > >My Favorite Baseball Player =3D George Herman "Babe" Ruth
> > >What did your do on Christmas =3D I rested, computed the % mortgage an=
d
> > >visited my brother + sister.
> > >Favorite Curse =3D That umpire is a #&*%!
>
> > >What I need is a way to specify valid characters.
>
> > >VariableName: Alphanumeric (and perhaps underscore), blank space.
> > >VariableValue: Pretty much anything is valid on the RHS except an '=3D=
'
> > >sign (I guess)
>
> > >Thanks for your kind assistance.
>
> > >-Ramon
>
> > -sln
>
> > use strict;
> > use warnings;
>
> > my $buf =A0=3D '';
>
> > while (<DATA>)
> > {
> > =A0 =A0 =A0 =A0 if (/=3D/ or eof) {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ($buf =3D~ /\s*([\w ]+)\s*=3D\s*((?:=
 .+(?:\n .+)*)|)/)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 my ($var,$val) =3D ($1,=
$2);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $val =3D~ s/\n +/\n/g;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "$var =3D> $val\n=
\n";
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $buf =3D '';
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 $buf .=3D $_; =A0 =A0}
>
> > __DATA__
>
> > My Favorite Baseball Player =3D George Herman =3D =A0"Babe" Ruth
> > What did your do on Christmas =3D I rested, computed the % mortgage and
> > =A0visited my brother + sister.
> > =A0asdfasdf=3D
> > Favorite Curse =3D That umpire is a #&*%!
> > errnngsf
> > sngdnsdg
> > Describe your summer vacation =3D Well, we traveled to the beach
> > =A0 and to the mountains, and debated whether we
> > =A0 should go to the Grand Canyon and Niagara falls.
> > =A0 The GPS you gave me turned out to be very useful!
>
> Thank you, sln!
>
> I have to clarify that my program is not written in Perl (language
> that I haven't used in ages) but in C++. The reason I posted my
> question in this NG will be understood by reading this:
>
> http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/...
>
> I am sticking with the default (Perl) Regex syntax.
>
> This is the relevant code that I have so far. As you can see it is
> rather simplistic. I am not implementing continuation lines yet.
>
> const string variable =3D "([\\w ]+)";
> const char equal_sign =3D '=3D';
> const string value =A0 =A0=3D "([\\w ]+)";
>
> const string assignment =3D variable + equal_sign + value;
>
> The question that I have is this: how do I restrict the LHS to begin
> with an alphabetic characters? IOW: The LHS may contain blanks but
> they cannot be the first character of the line. I will also be
> accepting digits, periods and underscores on the LHS but again, the
> variable name cannot begin with any of them.
>
> TIA,
>
> -Ramon

I have made some progress here:

const string variable =3D "(\\w+[\\w\\d\\. ]*)";
const char equal_sign =3D '=3D';
const string value    =3D "(.+)";

I think the above will cover most real cases, but not sure what will
happen if the RHS contains an '=3D' sign?

-RFH



------------------------------

Date: Tue, 08 Sep 2009 15:44:27 -0700
From: sln@netherlands.com
Subject: Re: Need expert help matching a line
Message-Id: <h7nda55idvtec7ts3la2ete82m4fp2c4f3@4ax.com>

On Tue, 8 Sep 2009 15:20:04 -0700 (PDT), Ramon F Herrera <ramon@conexus.net> wrote:

>On Sep 8, 1:35 pm, s...@netherlands.com wrote:
>> On Tue, 8 Sep 2009 05:23:32 -0700 (PDT), Ramon F Herrera <ra...@conexus.net> wrote:
>>
<snip>
>I have to clarify that my program is not written in Perl (language
>that I haven't used in ages) but in C++. The reason I posted my
>question in this NG will be understood by reading this:
>
>http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/syntax.html
>
>I am sticking with the default (Perl) Regex syntax.
>
This uses Perl 5.8 as a reference to describe the syntax. That is its library default?
http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

>This is the relevant code that I have so far. As you can see it is
>rather simplistic. I am not implementing continuation lines yet.
>
>const string variable = "([\\w ]+)";
>const char equal_sign = '=';
>const string value    = "([\\w ]+)";
>
>const string assignment = variable + equal_sign + value;
>
>The question that I have is this: how do I restrict the LHS to begin
>with an alphabetic characters? IOW: The LHS may contain blanks but
>they cannot be the first character of the line. I will also be
>accepting digits, periods and underscores on the LHS but again, the
>variable name cannot begin with any of them.
>
To just add those restrictions just requires this:
const string variable = "([a-zA-Z][\\w. ]+)";

There is nothing in the regex that is not in Perl 5.8, if
thats what they will be using.

-sln


------------------------------

Date: Tue, 8 Sep 2009 16:51:00 -0700 (PDT)
From: Ramon F Herrera <ramon@conexus.net>
Subject: Re: Need expert help matching a line
Message-Id: <7c28ec27-1f3e-4ef2-926f-8226ae019786@a21g2000yqc.googlegroups.com>


On Sep 8, 6:44=A0pm, s...@netherlands.com wrote:

 > This uses Perl 5.8 as a reference to describe the syntax.
 > That is its library default?

Don't know, but frankly, my expressions will be so simple (the only
ones I am capable of writing :-) that I doubt the syntax version will
make any difference.

All these questions are really to refresh my memory, since all this
stuff is coming back to me. My biggie Perl pattern matching project
was as follows. I used to manage a multi-thousand subscriber mailing
list at MIT. Those days e-mail traffic could really bog down a server
and network, and many of my subscribers graduated or went on a summer
vacation, etc., and forgot to unsubscribe. What I did was to pattern-
match every conceivable bounce received and extract the e-mail addres
of the subscriber. There were lots of mail servers then, BITNET, UUCP,
DECNET and many versions of sendmail. At least today e-mail addresses
are pretty much standard.

-Ramon



------------------------------

Date: Tue, 08 Sep 2009 11:36:57 -0700
From: sln@netherlands.com
Subject: Re: Perl regex expression to return values
Message-Id: <0s8da59bcinktdvlbj4atdtq6ngtgl6d46@4ax.com>

On Tue, 8 Sep 2009 10:04:34 -0700 (PDT), ian <ipellew@yahoo.com> wrote:

>Cut un paste and you will see the problem
>Code comment shows what I can't do !!
>NOTE! DATA is 2 (TWO) lines only.
>
>#!/usr/bin/perl
use strict;
use warnings;

>my $n = "\n";
>while ( <DATA> ) {
>    chomp;
>        my ( @bu ) = $_ =~ m{
>                              .*
>                              (\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d)
>                              .\d*\s
>                              ([\w]*)[\W]
>                              (.*-\s*)
>                              (Returning\sfrom|Entering)\s* 
>                              ([\w._]+)

                              (\(.*?\))
                              (,\s+[\w]+:\s+|)
                              (.*|)

>                            }x;
>        my $c = 0;
>        for my $q ( @bu ) {
>            $c++;
>            printf "%2s '%s'\n", $c, $q;
>        }
>    print $n;
>    next;
>}
>print "-----------".$n;
>

-sln


 1 '2009-09-01 15:58:51'
 2 'INFO'
 3 ' [ com.manager ]  - '
 4 'Entering'
 5 'get.Prod_Codes'
 6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)'
 7 ''
 8 ''

 1 '2009-09-01 15:58:51'
 2 'INFO'
 3 ' [ com.manager ]  - '
 4 'Returning from'
 5 'get.Prod_Codes'
 6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)'
 7 ', result: '
 8 'id:017661, productCodes: [GEAR | BOX | TARGET (2000)], Descn: [Mechan  | HAR
DENED  | Type 1 (2010)  ])'

-----------




------------------------------

Date: Tue, 08 Sep 2009 12:56:06 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Perl regex expression to return values
Message-Id: <mFypm.413153$Ta5.365158@newsfe15.iad>

ian wrote:
> Cut un paste and you will see the problem
> Code comment shows what I can't do !!
> NOTE! DATA is 2 (TWO) lines only.
> 
> #!/usr/bin/perl
> my $n = "\n";
> while ( <DATA> ) {
>     chomp;
>         my ( @bu ) = $_ =~ m{
>                               .*
>                               (\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d)
>                               .\d*\s
>                               ([\w]*)[\W]
>                               (.*-\s*)
>                               (Returning\sfrom|Entering)\s*       #
> Should be able to get 1 or n words
>                               ([\w._]+)
>                                                     # Problem Area !!
>                               (?:
>                                   (\(.*\))
>                                 |
>                                   (\(.*\))
>                                   (,\s+[\w]+:\s+)
>                                   (.*)
>                               )
>                             }x;
>         my $c = 0;
>         for my $q ( @bu ) {
>             $c++;
>             printf "%2s '%s'\n", $c, $q;
>         }
>     print $n;
>     next;
> }
> print "-----------".$n;

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

while ( <DATA> ) {
     chomp;
     my @bu = m{
             .*
             ( \d\d\d\d - \d\d - \d\d \s+ \d\d : \d\d : \d\d )
             . \d* \s
             ( \w* ) \W
             ( .* - \s* )
             ( Returning \s+ from | Entering ) \s*       # Should be 
able to get 1 or n words
             ( [\w._]+ )
             ( \( [^()]* \) )
             (?:
                 ( , \s+ \w+ : \s+ )
                 ( .* )
             )?
         }x;

     my $c;
     for my $q ( @bu ) {
         next unless defined $q;
         printf "%2s '%s'\n", ++$c, $q;
         }
     print "\n";
     next;
     }
print "-----------\n";

__DATA__
Server.log:2009-09-01 15:58:51,513 INFO  [ com.manager ]  - Entering 
get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, 
instance:TEST)
Server.log:2009-09-01 15:58:51,531 INFO  [ com.manager ]  - Returning 
from get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, 
instance:TEST), result: (id:017661, productCodes: [GEAR | BOX | TARGET 
(2000)], Descn: [Mechan  | HARDENED  | Type 1 (2010)  ])




John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


------------------------------

Date: Tue, 08 Sep 2009 12:11:44 -0700
From: sln@netherlands.com
Subject: Re: replace words
Message-Id: <otada5p8rpk9cs3ah9i191knmcvrs2kkpq@4ax.com>

On Mon, 7 Sep 2009 16:51:55 -0700 (PDT), fred <fred78980@yahoo.com> wrote:

>Thanks sln from @netherlands.com/
>
>xxxx&(ght)(hgf)&&(yyt)
>xx9x&(gg)(ff)&&(yyt)
>oixxx&(hfd)(jj)&&(yyt)
>xxxx&(jj)(kk)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>
>This command is working. I can change && for foo1 and foo2.
>perl -0 -pe 's(&&)(++$n < 2 ? "&foo1&" : "&foo2&")eg' text.txt
>
>Please explain to me why the command is not working for foo3.
>perl -0 -pe 's(&&)(++$n < 2 ? "&foo1&" : "&foo2&" : "&foo3&" )eg'
>text.txt
>Thanks

Looks like your trying to do this, but not sure.

perl -0 -pe 's(&&)(++$n; "&foo$n&")eg'

xxxx&(ght)(hgf)&foo1&(yyt)
xx9x&(gg)(ff)&foo2&(yyt)
oixxx&(hfd)(jj)&foo3&(yyt)
xxxx&(jj)(kk)&foo4&(yyt)
xjhxxx&(jj)(j)&foo5&(yyt)
xjhxxx&(jj)(j)&foo6&(yyt)
xjhxxx&(jj)(j)&foo7&(yyt)

-sln


------------------------------

Date: Tue, 08 Sep 2009 11:23:55 -0700
From: sln@netherlands.com
Subject: Re: XML::Parser
Message-Id: <n48da5du5snlmldmdr1oob2p6arna0rf56@4ax.com>

On Tue, 08 Sep 2009 13:48:19 -0400, monkeys paw <user@example.net> wrote:

>How can self ending elements be detected using
>the parser?
>
>Such as <br/> will fire both a start handler and
>an end handler. I need to know that it differs from
>a normal tag such as <start>text</start>.

I wrote a parser that I send a flag to the handler.
Most parsers have a original_string() method that
you can check closure yourself.

-sln


------------------------------

Date: Tue, 08 Sep 2009 13:51:26 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: XML::Parser
Message-Id: <slrnhad97r.16l.tadmc@tadmc30.sbcglobal.net>

monkeys paw <user@example.net> wrote:

> How can self ending elements be detected using
          ^^^^^^^^^^^^^^^^^^^^
> the parser?


The are called "empty elements".


> Such as <br/> will fire both a start handler and
> an end handler. 


Because

   <br/>

and

   <br></br>

are *equivalent* in XML, they do *not* differ.

You are free to write either one, and they have the same meaning.


> I need to know that it differs from


Then you are not treating it as XML.


> a normal tag such as <start>text</start>.


Errr, non-empty elements are ... not empty!

Look to see if there is any content.



I am pretty sure this is an XY-problem.

What are you really trying to accomplish?

That is, why do you think you need to be able to distinguish
between <br/> and <br></br>?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Tue, 08 Sep 2009 16:09:33 -0500
From: Lawrence Statton <yankeeinexile@gmail.com>
Subject: Re: XML::Parser
Message-Id: <m1vdjtp2ia.fsf@mac.gateway.2wire.net>

Tad J McClellan <tadmc@seesig.invalid> writes:
> That is, why do you think you need to be able to distinguish
> between <br/> and <br></br>?

I had the same problem as OP once some years ago.  I can't remember how
I solved it, but that was in the "bad old days" when I was still using
XML::Parser and friends, and I did solve it.

As to "why" one would want to differentiate between <foo/> and
<foo></foo>, in my situation I reading in a serialization stream from a
database dump, where the first tag was used to represent NULL and the
other to represent the empty-string.

That applying these semantics to XML tags was a incompatible with the
XML standard was lost on the long-dead[1] author of the dump tool.

[1]  He wasn't actually dead, but was just as unavailable to fix his
error as if he had been dead.


--
Lawrence


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2590
***************************************


home help back first fref pref prev next nref lref last post