
Chris Charley
User
Oct 22, 2011, 10:07 AM
Post #14 of 16
(1424 views)
|
|
Re: [hervebags] How to parse this file?
[In reply to]
|
Can't Post
|
|
Hello Herve, Here are some corrections to your most recent code post. #!/usr/bin/perl use warnings; use strict; open my $FILE1, "<x.txt" or die "Can not open file1"; # Good! But the 3 argument form of open is preferred for (read or write). # Also, you should include the '$!' variable in your die statement. # $! will give additional info on where the die occurred (line number). # open my $FILE1, "<", "x.txt" or die "Can not open x.txt. $!"; open my $FILE2, ">out.x" or die "Can not create file2"; # open my $FILE2, ">", "out.x" or die "Can not create out.x. $!"; while (my $line = $FILE1) { # You are missing the file read *angle* brackets # while(my $line = <$FILE1>) my @words = split /\d+/,$line; # split doesn't capture digits here, it throws them away. # I will attach a program below that solves this parsing problem. # split is not the right tool for this data. # use Data::Dumper to see whats in your @words. You'll be surprised! # Data::Dumper is a good debugging tool to view what is in a variable. use Data::Dumper; print Dumper \@words; my $line_out; foreach my $word (@words) { if ($word =~ m/^(\+|-){0,1}\d.(0|1|2|3|4|5|6|7|8|9){0,}(E){0,1}(\+|-){0,1}.{0,3}/) # ^ ^ # You had 2 errors in your reg. expr. I marked the corrections (line above). # could write more simply as: $word =~ /([-+]?\d+\.\d{15}(?:E[-+]\d{3})?)/ { $line_out .= $word . ' '; } } #print $FILE2 "$line_out\n"; } close $FILE1 or die "Unable to close x.txt. $!"; close $FILE2 or die "Unable to close out.x. $!"; exit(0); # usually never needed. Not needed here. __END__ Data looks like: (6.278305337995641,-31.165930909567250) (1.758723282034028,-1.229391225298848) (1.133464180020400,-3.392317005428029E-001) (3.528019029863541,9.833493189283731E-001) (-1.589297342018588,-6.805368090065715E-001) (-5.740909803957738E-002,5.972384666809710E-001) And here is code I would have used to parse this data. #!/usr/bin/perl use strict; use warnings; # open my in_file handle open my $in_fh, "<", "x.txt" or die "Cannot open x.txt for reading. $!"; open my $out_fh, ">", "out.txt" or die "Cannot open out.txt for writing. $!"; while (my $line = <$in_fh>) { for ($line ) { s/^\(//; # lose the opening parenthesis s/\)$//; # lose the closing parenthesis s/,/ /; # substitute a space for the comma } print $out_fh $line; } close $in_fh or die "Unable to close x.txt from reading. $!"; close $out_fh or die "Unable to close out.txt for writing. $!"; __END__ raw data looks like (6.278305337995641,-31.165930909567250) (1.758723282034028,-1.229391225298848) (1.133464180020400,-3.392317005428029E-001) (3.528019029863541,9.833493189283731E-001) (-1.589297342018588,-6.805368090065715E-001) (-5.740909803957738E-002,5.972384666809710E-001) A few other suggestions/advice. To post to this site, I follow these steps. 1. At the bottom of the posting window, there is a button that says ''Switch to Basic Editor'. I found this easier to use than the 'Advanced Editor'. 2. Don't try to markup with your own labels. Use the 'code' button at the bottom of the editing box to precede code and when you paste in your code, you'll note that the 'code' button changes to '/code'. Click it then (at the end of your code). 3. Don't try to type in other people's code - it is too easy to make a mistake. Instead, at the top of their post, click on the 'Quote' option. Then, when the window comes up: - click inside the (box) with the post. - Press Contrl + A to select all the post - Press Contrl + C to copy - In your text editor, on a blank page, click the empty page. - Press Contrl + V to paste the post into your editor. - Edit out text, etc. leaving just the code. Here is a recent post from another forum that is pretty good advice for someone just starting with Perl. There are many ways to learn Perl. I prefer printed books, supplemented with online documentation (perldoc, FAQ, tutorials), various WWW sites, and mailing lists. And, of course, lots of practice. The canonical book for learning Perl is "Learning Perl": http://shop.oreilly.com/product/0636920018452.do "Perl Cookbook" is a well-organized guide book full of practical code snippets for solving common programming chores ("what" and "how"). The explanations are excellent ("why"). This book will take you from writing toy scripts to writing really useful scripts: http://shop.oreilly.com/product/9780596003135.do "Programming Perl" is the canonical Perl reference book. A new version is due out in December: http://shop.oreilly.com/product/9780596004927.do I recommend that you get the first two right away. Just another note. The documention on regular expressions can be dense, repititious, and not as easy to read as other sections of the docs - so don't get discouraged. Get familiar with it and see if you can find the specific section that deals with the problem you're having. (perldoc perlretut, perldoc perlrequick, perldoc perlre, and a couple more :-) ). I keep my book 'Programming Perl' here at my desk as it is a great book to learn the language (in addtion to the other books mentioned above). Chris
(This post was edited by Chris Charley on Oct 22, 2011, 1:37 PM)
|