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:
extracting data between string

 



dani8586
Novice

Jun 18, 2013, 5:17 AM

Post #1 of 12 (777 views)
extracting data between string Can't Post

Hi,
i have the following line in a file (file.dat)

DECAY 6100006 7.08092110e-05 # t1 decays

and I need to extract the 7.08092110e-05 and both print it at video and save it into file.
I have tried

open(file "file.dat");
while(<file>){
print $1 if /DECAY 6100006 (.*\n)/;
}
close(file);

but this just give me

7.02489719e-05 # t2 decays

while I just need the number.. is there a way to do this?

Cheers


g4143
Novice

Jun 18, 2013, 6:02 AM

Post #2 of 12 (768 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post

You really don't give us enough information about your datafile example but you could start here:


Code
#!/usr/bin/perl 

use warnings;
use strict;
use autodie qw/open close/;

print "Enter filename->";
chomp(my $ifilename = <STDIN>);

open(my $IFILE, "<", $ifilename);

while ( <$IFILE> )
{
chomp;
#found regexp here http://stackoverflow.com/questions/2293780/how-to-detect-a-floating-point-number-using-a-regular-expression
if (/DECAY 6100006 ((([1-9][0-9]*\.?[0-9]*)|(\.[0-9]+))([Ee][+-]?[0-9]+)?)/)
{
print $1 , "\n";
}
}

close($IFILE);

__END__



(This post was edited by g4143 on Jun 18, 2013, 6:08 AM)


BillKSmith
Veteran

Jun 18, 2013, 6:04 AM

Post #3 of 12 (764 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post

Real numbers can get tricky. Use the module Regexp::Common.

Code
use strict; 
use warnings;
use Regexp::Common qw( number );

while(<DATA>){
print $1 if /DECAY 6100006 ($RE{num}{real})/;
}
__DATA__
DECAY 6100006 7.08092110e-05 # t1 decays


UPDATE: Removed unnecessary code and fixed typo.
Good Luck,
Bill

(This post was edited by BillKSmith on Jun 18, 2013, 6:11 AM)


dani8586
Novice

Jun 18, 2013, 8:10 AM

Post #4 of 12 (750 views)
Re: [g4143] extracting data between string [In reply to] Can't Post

Thank you both for the quick answer... Unfortunately it doesn't work... I have a problem both with "strict" and whit the printing... It just doesn't print anything...
I remember there was some sintax with the dollars in order to print something before or after a certain string.. Do you know if I'm wrong or if this is true?

In Reply To


BillKSmith
Veteran

Jun 18, 2013, 9:17 AM

Post #5 of 12 (742 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post

Both g.... and I posted complete programs that answer your original question and do execute under strict. (Try both of them).

Make strict your friend. It tells perl not to allow undeclared variables or symbolic references. This detects many common errors (such as misspelled variable names). You do have to declare all your variables (almost always with my). Declare your variables in the smallest possible scope to gain the largest advantage.

I cannot guess what your print problem is without seeing your code and data. Are you checking to verify that your file did open successfully? Are you checking that your match succeeded? I suspect that one of these is failing.
Good Luck,
Bill


dani8586
Novice

Jun 18, 2013, 9:25 AM

Post #6 of 12 (740 views)
Re: [BillKSmith] extracting data between string [In reply to] Can't Post

Thanks...
I have tryed many things other the one you suggested. I don't think is a problem of file opening because in the code



open(FILE, "$file.dat");
while(<FILE>){
print $1 if /DECAY 6100006 ($RE{num}{real})/;
print $1 if $line =~ /DECAY 6100006 (\S+)/;
print $1 if /DECAY 6100006 (.*\n)/;
}
close(FILE);

just the third one works, but print it the
7.08092110e-05 # t1 decays
while the first two don't print anything ( I have also tried to print them one per time..)

So I don't really know, I'll try to solve the problem in other ways.. thanks a lot for your time!



In Reply To


FishMonger
Veteran / Moderator

Jun 18, 2013, 9:55 AM

Post #7 of 12 (737 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post

Here's a perfect example why you should always put your code and sample date within code tags.

After looking at the source of the html page, I see that there are multiple spaces between the fields. That is what is causing it to fail.

Here's the actual line of data.

Code
DECAY   6100006   7.08092110e-05   # t1 decays



FishMonger
Veteran / Moderator

Jun 18, 2013, 9:57 AM

Post #8 of 12 (736 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post


Code
open my $fh, '<', "file.dat" or die "failed to open 'file.dat' $!"; 

while( my $line = <$fh>){
chomp $line;
print "$1\n" if $line =~ /DECAY \s+ 6100006 \s+ (\S+)/x;
}

close($fh);



dani8586
Novice

Jun 18, 2013, 10:02 AM

Post #9 of 12 (734 views)
Re: [FishMonger] extracting data between string [In reply to] Can't Post


In Reply To
Here's a perfect example why you should always put your code and sample date within code tags.

After looking at the source of the html page, I see that there are multiple spaces between the fields. That is what is causing it to fail.

Here's the actual line of data.

Code
DECAY   6100006   7.08092110e-05   # t1 decays



Hi thanks, what does it mean multiple spaces? and what are code tags?


FishMonger
Veteran / Moderator

Jun 18, 2013, 10:18 AM

Post #10 of 12 (733 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post


Quote
Hi thanks, what does it mean multiple spaces?

What??

A space is produced by that long key along the bottom of your keyboard. There are multiple spaces (key presses) between each field in your string.


Quote
and what are code tags?

Your own post used the quote tags when responding to my post. The code tag is just to the left of the quote tag.

The code tag will retain the formatting of the string.


BillKSmith
Veteran

Jun 18, 2013, 1:29 PM

Post #11 of 12 (726 views)
Re: [dani8586] extracting data between string [In reply to] Can't Post

Your first regexp failed to match because you failed to use the module. Use strict would have found the problem at compile time. Fix that, and it will fail at runtime because it demands exactly one space between 6100006 and your number. Your new input line has three.

Your second regexp is looking for the number in an undefined string $line. Again use strict would have found the error at compile time.

Your third regexp would also fails with the new data because of the extra spaces. (You report that it worked. You were probably using the original data)

Turning off strict does not make the errors go away, only the message. You should not be suprised when a program with errors does not work as intended. What would you expect if your doctor treated your broken leg by fixing the x-ray?
Good Luck,
Bill


dani8586
Novice

Jun 18, 2013, 1:54 PM

Post #12 of 12 (724 views)
Re: [BillKSmith] extracting data between string [In reply to] Can't Post

Smile

Thanks a lot for your help SmileSmile

 
 


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

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