[26121] in Perl-Users-Digest
Perl-Users Digest, Issue: 8314 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 11 14:05:28 2005
Date: Thu, 11 Aug 2005 11:05:07 -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 Thu, 11 Aug 2005 Volume: 10 Number: 8314
Today's topics:
Manually parsing quoted characters <krevlar.newsgroups@tragetaschen.dyndns.org>
Re: Manually parsing quoted characters <zen13097@zen.co.uk>
Re: Manually parsing quoted characters <krevlar.newsgroups@tragetaschen.dyndns.org>
Re: Manually parsing quoted characters (Gary E. Ansok)
Re: Naive Unix Socket client <news@LearnQuick.com>
Reference to hash element <konrad@gaisler.com>
Re: Reference to hash element <zen13097@zen.co.uk>
regex in perl (using variables) <dariosmece@yahoo.com>
Re: regex in perl (using variables) <noreply@gunnar.cc>
Re: regex in perl (using variables) <dariosmece@yahoo.com>
Re: regex in perl (using variables) <noreply@gunnar.cc>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 11 Aug 2005 12:16:09 +0200
From: Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org>
Subject: Manually parsing quoted characters
Message-Id: <ddf8h9$4sp$04$1@news.t-online.com>
Cheers,
is there some simpler/more elegant solution to the following problem:
given a string 'a\|b|c', transform it into the list ('a|b', 'c')
Currently, i have a intermediate representation, but i doubt its
generality. What if $string contains (out of reasons I cannot predict)
'{[[VerticalBar]]}' right from the start?
I need a primitive pattern-language, and currently it contains, among others,
(..|..|..) : Disjunction
which I have to parse to a list of its elements (w/o the |'s).
Arne Ruhnau
Code follows:
use strict;
use warnings;
use Test::More tests => 1;
my $string = 'a\|b|c';
is_deeply(string2list($string), ['a|b', 'c']);
sub string2list {
my $string = shift;
$string =~ s/\\\|/{[[VerticalBar]]}/g;
my @elements = split /\|/, $string;
for(@elements) {
s/\{\[\[VerticalBar\]\]\}/|/g;
}
return \@elements;
}
------------------------------
Date: 11 Aug 2005 11:18:58 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Manually parsing quoted characters
Message-Id: <42fb3422$0$18200$db0fefd9@news.zen.co.uk>
Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org> wrote:
> Cheers,
>
> is there some simpler/more elegant solution to the following problem:
>
> given a string 'a\|b|c', transform it into the list ('a|b', 'c')
>
Here's my attempt, splitting using a negative lookbehind assertion,
i.e. only splitting on a | if it's not preceded by a \
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @list = map { s/\\\|/|/g; $_ } split /(?<!\\)\|/, 'a\|b|c';
print Dumper \@list;
__END__
$VAR1 = [
'a|b',
'c'
];
------------------------------
Date: Thu, 11 Aug 2005 15:38:03 +0200
From: Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org>
Subject: Re: Manually parsing quoted characters
Message-Id: <ddfkbs$h07$00$1@news.t-online.com>
Dave Weaver wrote:
> Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org> wrote:
>
>> is there some simpler/more elegant solution to the following problem:
>>
>> given a string 'a\|b|c', transform it into the list ('a|b', 'c')
>
> Here's my attempt, splitting using a negative lookbehind assertion,
> i.e. only splitting on a | if it's not preceded by a \
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @list = map { s/\\\|/|/g; $_ } split /(?<!\\)\|/, 'a\|b|c';
Nice. I changed it to
map { s/\\(.)/$1/g; $_ } to be able to work with different quoted characters.
Thanks,
Arne Ruhnau
------------------------------
Date: Thu, 11 Aug 2005 17:51:01 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: Manually parsing quoted characters
Message-Id: <ddg365$qfa$1@naig.caltech.edu>
In article <42fb3422$0$18200$db0fefd9@news.zen.co.uk>,
Dave Weaver <zen13097@zen.co.uk> wrote:
>Arne Ruhnau <krevlar.newsgroups@tragetaschen.dyndns.org> wrote:
>> is there some simpler/more elegant solution to the following problem:
>> given a string 'a\|b|c', transform it into the list ('a|b', 'c')
>
>Here's my attempt, splitting using a negative lookbehind assertion,
>i.e. only splitting on a | if it's not preceded by a \
>
>#!/usr/bin/perl
>use strict;
>use warnings;
>use Data::Dumper;
>
>my @list = map { s/\\\|/|/g; $_ } split /(?<!\\)\|/, 'a\|b|c';
>print Dumper \@list;
>
>__END__
>
>$VAR1 = [
> 'a|b',
> 'c'
> ];
That runs into the question of how the string 'a\\|b|c' should be
treated -- should it result in [ 'a\', 'b', 'c' ] ? If so, then
a little more work needs to be put into the code.
(note for the nit-pickers: strings above indicate actual string
contents, not Perl string literals or Dumper output. Do we have
a convention for that?)
Gary
--
The recipe says "toss lightly," but I suppose that depends
on how much you eat and how bad the cramps get. - J. Lileks
------------------------------
Date: Thu, 11 Aug 2005 13:35:15 GMT
From: "Herb Martin" <news@LearnQuick.com>
Subject: Re: Naive Unix Socket client
Message-Id: <nuIKe.114370$gL1.100901@tornado.texas.rr.com>
I found this through the Cygwin list (and a very kind person who helped
me.)
It isn't a Perl solution but it works and answers the need many may have...
> socat using the UNIX-CONNECT: or UNIX-LISTEN: parameters ought to
> work.
> It is not a Cygwin package but it does build without much hassle.
Mine built fine under Cygwin, but failed some (named pipe) tests --
it works fine for the Unix sockets and presumably some other types
of connections.
I found it by searching SourceForge.org; even though there
is no code stored on Sourceforge their is a link to the
socat home page which has the code available.
socat Home Page:
http://www.dest-unreach.org/socat/
Direct link to gzip:
http://www.dest-unreach.org/socat/download/socat-1.4.2.0.tar.gz
--
Herb Martin
------------------------------
Date: Thu, 11 Aug 2005 11:39:56 GMT
From: Konrad Eisele <konrad@gaisler.com>
Subject: Reference to hash element
Message-Id: <gOGKe.143677$dP1.498116@newsc.telia.net>
is is possible to reference a element inside a hash?
something like:
%h = ( a => 1);
$a = \%h{'a'};
$a = 2;
=> $h{'a'} would be 2
------------------------------
Date: 11 Aug 2005 11:58:11 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Reference to hash element
Message-Id: <42fb3d53$0$24647$db0fefd9@news.zen.co.uk>
Konrad Eisele <konrad@gaisler.com> wrote:
> is is possible to reference a element inside a hash?
> something like:
>
> %h = ( a => 1);
> $a = \%h{'a'};
^
The hash element is $h{'a'}, so to take a reference to it:
$a = \$h{'a'}
Now $a is a reference to the hash element. To assign to the
referenced element you must dereference $a by using an extra
'$' prefix:
$$a = 2;
Alternatively, you could use a for() to create a temporary
alias to the element:
for my $a ( $h{'a'} ) {
$a = 2;
}
------------------------------
Date: Thu, 11 Aug 2005 13:56:15 +0200
From: "dario" <dariosmece@yahoo.com>
Subject: regex in perl (using variables)
Message-Id: <ddfeaf$73h$1@bagan.srce.hr>
How do I make this work!!!
$head_ ="Subject: Get cheap v i a g r a ..... ";
#$rule is a variable which I used for reading text from a file!
open (NWRULE, "<rule.spam");
@new_rule=<NWRULE>;
close (NWRULE);
Then I did sometning like this :
foreach $rule(@new_rule)
{
if($rule =~ /(\S+) (\S+) ([^\n]+)/)
{
$new_id=$1;
$dio=$2;
$reg=$3;
if($head_ =~ m/$reg/)
{
print "something\n";
}
....
Content of a file rule.spam is :
new_1 head Subject: .*\.\.
------------------------------
Date: Thu, 11 Aug 2005 14:19:04 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex in perl (using variables)
Message-Id: <3m0u1uF14p6drU1@individual.net>
dario wrote:
> How do I make this work!!!
<fragmentary code snipped>
Please post a _short_ but _complete_ program that illustrates the
problem you are having, just as is explained in the posting guidelines
for this group.
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 11 Aug 2005 14:35:21 +0200
From: "dario" <dariosmece@yahoo.com>
Subject: Re: regex in perl (using variables)
Message-Id: <ddfgjs$g85$1@bagan.srce.hr>
Sorry about the previuos post, i hope this is better!
I want to match regex stored in a file to a text in a variable $head_. It
works in windows, but not on linux.
Thanks!
Dario
Content of a file rule.spam is :
new_1 head Subject: .*\.\.
Code is:
$head_ ="Subject: Get cheap v i a g r a ..... ";
open (NWRULE, "<rule.spam");
@new_rule=<NWRULE>;
close (NWRULE);
foreach $rule(@new_rule)
{
if($rule =~ /(\S+) (\S+) ([^\n]+)/)
{
$new_id=$1;
$dio=$2;
$reg=$3;
}
if($head_ =~ m/$reg/)
{
print "something\n";# it doesn't match
}
}
------------------------------
Date: Thu, 11 Aug 2005 18:16:51 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex in perl (using variables)
Message-Id: <3m1bvqF13qv49U1@individual.net>
[ Please provide context when replying to a message. ]
dario wrote:
> Gunnar Hjalmarsson wrote:
>> dario wrote:
>>> How do I make this work!!!
>>
>> <fragmentary code snipped>
>>
>> Please post a _short_ but _complete_ program that illustrates the
>> problem you are having, just as is explained in the posting guidelines
>> for this group.
>> http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
>
> Sorry about the previuos post, i hope this is better!
> I want to match regex stored in a file to a text in a variable $head_. It
> works in windows, but not on linux.
> Thanks!
> Dario
>
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
> Code is:
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> }
> if($head_ =~ m/$reg/)
> {
> print "something\n";# it doesn't match
> }
> }
That's still not a complete program that people can copy, paste and run
as is suggested in the posting guidelines. The below code is (I
think...). Note: strictures and warnings enabled; input data provided
via the __DATA__ token.
OTOH, the below program prints the expected result, so you wouldn't have
needed to post it. But if you had written it, you could have concluded
that what's probably causing your program to fail is that the open()
statement fails. Applying one of 'the golden rules', i.e. checking the
return value of open(), would likely have told you that as well.
#!/usr/bin/perl
use strict;
use warnings;
my $head_ ="Subject: Get cheap v i a g r a ..... ";
while ( my $rule = <DATA> ) {
my ($new_id, $dio, $reg);
if ( $rule =~ /(\S+) (\S+) ([^\n]+)/ ) {
$new_id=$1;
$dio=$2;
$reg=$3;
}
if ( $head_ =~ m/$reg/ ) {
print "something\n";
}
}
__DATA__
new_1 head Subject: .*\.\.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
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 V10 Issue 8314
***************************************