extract variables from expression

M

Matt Garrish

Hi,

Does somebody know how to extract some variables from this expression :

alexandre said:
Ceci est un testttttt

I want to extract the values alexandre and the text "Ceci est un test"
and I can have multiple quoted expression :
alexandre said:
Ceci est un testttttt
tester said:

Assuming no nested quotes and the data is always as simple as the above:

while (my $line = <DATA>) {
if ($line =~ m#\[([^\]]+)](.*?)\[/quote]#) {
my $speaker = (split(/=/, $1))[1];
my $quote = $2;
}
}

__DATA__
alexandre said:
Ceci est un testttttt
tester said:
 
G

Gunnar Hjalmarsson

Does somebody know how to extract some variables from this expression :

alexandre said:
Ceci est un testttttt

I want to extract the values alexandre and the text "Ceci est un test"
and I can have multiple quoted expression :
alexandre said:
Ceci est un testttttt
tester said:

You can use a regular expression.

perldoc perlrequick
perldoc perlretut
perldoc perlre

Please read the posting guidelines for this group:
http://groups.google.com/group/comp.lang.perl.misc/msg/b1b69d3cbfeaa440
 
M

Matt Garrish

Matt Garrish said:
Hi,

Does somebody know how to extract some variables from this expression :

alexandre said:
Ceci est un testttttt

I want to extract the values alexandre and the text "Ceci est un test"
and I can have multiple quoted expression :
alexandre said:
Ceci est un testttttt
tester said:

Assuming no nested quotes and the data is always as simple as the above:

while (my $line = <DATA>) {
if ($line =~ m#\[([^\]]+)](.*?)\
#) {
my $speaker = (split(/=/, $1))[1];[/QUOTE]

I was going somewhere else with that code. It can be made much simpler and
reliable for this single case with the following:

if ($line =~ m#\
([^\ said:
]+)](.*?)\
#) {
my $speaker = $1;
my $quote = $2;
}
}

__DATA__
alexandre said:
Ceci est un testttttt
tester said:
 
A

alexjaquet

thx for responding but If I test the following :

sub postMessage {
local our $value = "
alexandre said:
Ceci est un testttttt
2eme reponse
alexandre2 said:
Ceci est un testttttt234
3eme
reponse";
local our ($speaker);
local our ($quote);
local our $line = $value;
for (local our $i = 0;$i < length($value);$i++) {
if ($value =~ m#\[([^\]]+)](.*?)\[/quote]#) {
$speaker = (split(/=/, $1))[1];
$quote = $2;
}
print "Content-Type: text/html\n\n";
print "$speaker\n";
print "$quote\n";
}
}
I only get the good result for one speaker and one quote :
http://community.no-ip.biz/cgi-bin/...2b4cbb1509cae8277f1e5fd5a&action=post_message
 
A

alexjaquet

I made another test :

#!/usr/bin/perl -w
use strict;

my $line = "
alexandre said:
Ceci est un
testttttt
tester said:
";
open(DATA,">filename.txt");
print DATA $line;
close DATA;
open(DATA,"<filename.txt");
while ($line = <DATA>) {
if ($line =~ m#\[([^\]]+)](.*?)\[/quote]#) {
my $speaker = (split(/=/, $1))[1];
my $quote = $2;
print "$speaker\n";
}

}
close DATA;

and it's only print the first speaker
 
M

Matt Garrish

thx for responding but If I test the following :

sub postMessage {
local our $value = "
alexandre said:
Ceci est un testttttt
2eme reponse
alexandre2 said:
Ceci est un testttttt234
3eme
reponse";
local our ($speaker);
local our ($quote);
local our $line = $value;
for (local our $i = 0;$i < length($value);$i++) {
if ($value =~ m#\[([^\]]+)](.*?)\
#) {
$speaker = (split(/=/, $1))[1];
$quote = $2;
}
print "Content-Type: text/html\n\n";
print "$speaker\n";
print "$quote\n";
}
}
I only get the good result for one speaker and one quote :
http://community.no-ip.biz/cgi-bin/...2b4cbb1509cae8277f1e5fd5a&action=post_message
[/QUOTE]

That's because I didn't write it to operate on a single string value. You
need to look into a while loop and the global match switch to get it working
the way you want. I would start with perlre.

Out of curiosity, what do you think that for loop is doing in the above, and
why is the header response *inside* it?

Matt
 
A

alexjaquet

"Out of curiosity, what do you think that for loop is doing in the
above, and
why is the header response *inside* it?"

Yes I did a shity example lol it's print html header in the loop ...
 
M

Matt Garrish

I made another test :

#!/usr/bin/perl -w
use strict;

my $line = "
alexandre said:
Ceci est un
testttttt
tester said:
";
open(DATA,">filename.txt");
print DATA $line;
close DATA;
open(DATA,"<filename.txt");
while ($line = <DATA>) {
if ($line =~ m#\[([^\]]+)](.*?)\
#) {
my $speaker = (split(/=/, $1))[1];
my $quote = $2;
print "$speaker\n";
}

}
close DATA;

and it's only print the first speaker
[/QUOTE]

Once again, post context when replying.

You need to step back and learn some basic Perl first. But since you're
making at least some effort:

my $line = "
alexandre said:
Ceci est un
testttttt
tester said:
";

while ($line =~ m#\
([^\ said:
]+)](.*?)\
#gs) {
my $speaker = $1;
my $quote = $2;
print "$speaker\n";
}

Matt
 
A

alexjaquet

ho great job Matt thank you very much

"You need to step back and learn some basic Perl first. But since
you're
making at least some effort: "

Yes I've to take back some books about regular expression
 
A

A. Sinan Unur


Why are you using a package scope variable for a simple loop variable?
if ($value =~ m#\[([^\]]+)](.*?)\
#) {
[/QUOTE]

What do you think this loop is doing?
$speaker = (split(/=/, $1))[1];
$quote = $2;
}
print "Content-Type: text/html\n\n";

The header should only be printed once.

Why do you lie to the web browser?

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top