CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Match string in multi-line file

 

First page Previous page 1 2 Next page Last page  View All


Laurent_R
Veteran / Moderator

Dec 31, 2012, 7:59 AM

Post #26 of 35 (1839 views)
Re: [Stefanik] Match string in multi-line file [In reply to] Can't Post

It should match. If it doesn't match, then you probably don't have what you think in your variable and you should try to print it.

Test of the regex under the debugger:



Code
  DB<1> $c = "TRANSID,valueNeeded" 

DB<2> print $2 if $c =~ /^(TRANSID),(\w+)/;
valueNeeded
DB<3> print "$1\n$2" if $c =~ /^(TRANSID),(\w+)/;
TRANSID
valueNeeded


As you can see, the regex does match the expression.


Stefanik
User

Jan 3, 2013, 1:32 PM

Post #27 of 35 (1803 views)
Re: [Laurent_R] Match string in multi-line file [In reply to] Can't Post

Thanks Laurent, I'll check.


Chris Charley
User

Jan 3, 2013, 3:43 PM

Post #28 of 35 (1795 views)
Re: [Stefanik] Match string in multi-line file [In reply to] Can't Post

Try changing this line

my $value1 = $2 if $FileTemp =~ /^(TRANSID),(\w+)/;

to

my $value1;
$value1 = $2 if $FileTemp =~ /^(TRANSID),(\w+)/m;



(This post was edited by Chris Charley on Jan 3, 2013, 3:45 PM)


BillKSmith
Veteran

Jan 3, 2013, 8:09 PM

Post #29 of 35 (1786 views)
Re: [Chris Charley] Match string in multi-line file [In reply to] Can't Post

Chris is certainly correct, but some explanation is needed. Editing multi-line files is a bit tricky. Finding perl's documentation of this flag is challenging and the result disappointing ("m Treat string as multiple lines." Refer: perldoc perlop). The book "Perl Best Practices" has a detailed explanation of this issue. The meat of it is that /m changes the meaning of the meta-characters ^ and $ to refer to the start/end of a line rather than the start/end of the string. (The book recommends always using the flags /xms)
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jan 3, 2013, 11:50 PM

Post #30 of 35 (1775 views)
Re: [Chris Charley] Match string in multi-line file [In reply to] Can't Post

Hmmm, granted, the /m flag will not do any harm here, but I do not see how it will be useful here. So far, except perhaps for a post title which might be misunderstood, there is nothing in this post to tell us that we have a multiline string at hand.


BillKSmith
Veteran

Jan 4, 2013, 5:17 AM

Post #31 of 35 (1770 views)
Re: [Laurent_R] Match string in multi-line file [In reply to] Can't Post

Note the code in post 25.

Code
while ($FileTemp = <PARSEFILE>){  
$FileTemp =~ s/:/\n/g;

The string is made into a multi-line string by changing the colons to newlines. We know that there are multiple colons from the file format in post 1.

UPDATE: The following code and output was added to this post to demonstrate the solution:


Code
use strict; 
use warnings;
*PARSEFILE = *DATA;
while (my $FileTemp = <PARSEFILE>){
$FileTemp =~ s/:/\n/g;
my $value1 = $2 if $FileTemp =~ /^(TRANSID),(\w+)/m;
print "The value of $1 is '$value1'\n" if defined $value1;
}
__DATA__
newline1:FieldCostant1,value1:FieldCostant2,value2:FieldCostant3,value3
newline4:FieldCostant1,value1:TRANSID,valueNeeded:FieldCostant3,value3
newline2:FieldCostant1,value1:FieldCostant2,value2:FieldCostant3,value3
newline3:FieldCostant1,value1:FieldCostant2,value2:FieldCostant3,value3

Output:

Code
The value of TRANSID is 'valueNeeded'

Good Luck,
Bill

(This post was edited by BillKSmith on Jan 4, 2013, 6:07 AM)


Laurent_R
Veteran / Moderator

Jan 4, 2013, 9:54 AM

Post #32 of 35 (1759 views)
Re: [BillKSmith] Match string in multi-line file [In reply to] Can't Post


In Reply To
Note the code in post 25.

Code
while ($FileTemp = <PARSEFILE>){  
$FileTemp =~ s/:/\n/g;

The string is made into a multi-line string by changing the colons to newlines. We know that there are multiple colons from the file format in post 1.


Right, I forgot that.


Stefanik
User

Jan 4, 2013, 1:08 PM

Post #33 of 35 (1747 views)
Re: [Chris Charley] Match string in multi-line file [In reply to] Can't Post

Chris what you suggest me works fine.

But I'm not sure about the problem of "/string/m" , Billk talks about.


Laurent_R
Veteran / Moderator

Jan 4, 2013, 4:10 PM

Post #34 of 35 (1741 views)
Re: [Stefanik] Match string in multi-line file [In reply to] Can't Post

If it works fine for ou, forget about the rest of the discussion.

Wink


BillKSmith
Veteran

Jan 4, 2013, 5:38 PM

Post #35 of 35 (1736 views)
Re: [Stefanik] Match string in multi-line file [In reply to] Can't Post

In post 29, I tried to explain why Chris's fix works. I consider perl's documentation of this issue very poor. I guess that I did not do any better, Sorry about that.

The update in my later post is the key part of your code with Chris's fix added. It is a complete program that anyone can run to prove that the fix works. It does not introduce any new ideas.
Good Luck,
Bill

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives