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:
Need to extract first line instead of second, and they both have same format

 



sparsh
Novice

Jun 12, 2015, 7:33 AM

Post #1 of 8 (2355 views)
Need to extract first line instead of second, and they both have same format Can't Post

I need to extract the dimensions of these two lines, using regular expressions. As I'm parsing through the file in a while loop, I manage to extract the second line and not the first. How do i get the first line, given both have the same format?

X= 489m, Y= 310m, A = 0.150m2
X= 387m, Y= 322m, A = 0.121m2


FishMonger
Veteran / Moderator

Jun 12, 2015, 7:43 AM

Post #2 of 8 (2354 views)
Re: [sparsh] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

Please post your code.

Why must you use a regex? You could do it via the split function.

EDIT:
Please include your input file, or a reasonable enough portion of it to give us a realistic understanding of the needed parsing.


(This post was edited by FishMonger on Jun 12, 2015, 7:52 AM)


sparsh
Novice

Jun 12, 2015, 7:45 AM

Post #3 of 8 (2352 views)
Re: [FishMonger] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

if ($node == 40)
{
if ($parsed_ds =~ m/^\s*(X=)\s*(\d+\.?\d*)(m)(,)\s*(Y=)\s*(\d+\.?\d*)(m)(,)\s*(A)\s*(=)\s*(\d+\.?\d*)(mm2)/)
{
$temp_x = $2;
$temp_y= $6;
$temp_area = $11;
}
}

}

print "$temp_x, $temp_y, $temp_area\n";


doing this gives me the second line and not the first.


FishMonger
Veteran / Moderator

Jun 12, 2015, 7:54 AM

Post #4 of 8 (2349 views)
Re: [sparsh] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

How and where is $parsed_ds being assigned?

I need a larger sample of your code. Preferably the whole script.


FishMonger
Veteran / Moderator

Jun 12, 2015, 7:57 AM

Post #5 of 8 (2348 views)
Re: [sparsh] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

Here's an example parsing method using the split function instead of a complex regex.

Code
use strict; 
use warnings;
use Data::Dumper;

while (<DATA>) {
chomp;
my %dimensions = split /[,= ]+/;
print Dumper \%dimensions;
}

__DATA__
X= 489m, Y= 310m, A = 0.150m2
X= 387m, Y= 322m, A = 0.121m2



sparsh
Novice

Jun 12, 2015, 10:56 AM

Post #6 of 8 (2341 views)
Re: [FishMonger] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

I got it working, since the X and Y dimensions as well as the area was always going to be larger on the first line, I just set a condition to check if the dimensions were larger than zero and then stored them.

Thanks for your help anyway


BillKSmith
Veteran

Jun 13, 2015, 3:28 AM

Post #7 of 8 (2264 views)
Re: [sparsh] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

Units are not the same in your first and second line. Your regular expression matches only the second.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 14, 2015, 8:23 AM

Post #8 of 8 (2068 views)
Re: [sparsh] Need to extract first line instead of second, and they both have same format [In reply to] Can't Post

You regex failed on the first line because your regex is explicitly looking for a "" at two different places, and there is no "" in the first line.

Asides from that, your regex is more complicated that it needs to be, you have a lot of useless parentheses. If you remove those useless parens, your numbers will end up in $1, $2 and $3.

Or you could assign directly the values. Try this (demonstrated under the Perl debugger):


Code
  DB<1> $parsed_ds = " X= 489m, Y= 310m, A = 0.150m2"; 

DB<2> ($x, $y, $z) = $parsed_ds =~ m/^\s*X=\s*([\d.]+).?m,\s*Y=\s*([\d.]+).?m,\s*A\s*=\s*([\d.]+).?m2/;

DB<3> p "$x $y $z";
489 310 0.150


You could also make it slightly more readable by using sub-patterns in variables:


Code
  DB<4> $num = qr /[\d.]+/; 

DB<5> ($x, $y, $z) = $parsed_ds =~ m/^\s*X=\s*($num).?m,\s*Y=\s*($num).?m,\s*A\s*=\s*($num).?m2/;

DB<6> p "$x $y $z";
489 310 0.150


Or even:


Code
$num = qr /([\d.]+)/; 
($x, $y, $z) = $parsed_ds =~ m/^\s*X=\s*$num.?m,\s*Y=\s*$num.?m,\s*A\s*=\s*$num.?m2/;


But, while it works well here, it is sometines not a good idea to include the parens in the subpatterns, because it can make the numbering of the matches confusing.

 
 


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

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