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:
How to parse this file?

 



hervebags
Novice

Oct 19, 2011, 6:38 AM

Post #1 of 16 (2261 views)
How to parse this file? Can't Post

Hi,

I have only been learning Perl for less than 2 weeks. I am a C++ programmer.
I have attached a portion of the data below. The data is in file1.txt. I would like to move the data from file1.txt to file2.txt. But, I only want to keep the numbers.

Eg: I want row 1 to look like this:

1 1549367 11 8 3 11 0 -12.00 6.00 -0.25 -3.00 0.00 -1.67 -12.00 6.00 -0.64

Instead of this:

1 Chr26 1549367 11 GGGGGGGAAGA 8 3 Transition 11 0 -12.00 6.00 -0.25 -3.00 0.00 -1.67 -12.00 6.00 -0.64

This is what I have done so far (file1.txt will be in @ARGV):

open FILE2, "+>file2.txt" or die "Cant not open file2.txt!";
my $line;
while($line = readline(ARGV))
{
print FILE2 $line;
}


The code above only copies content of file1.txt (ARGV) into file2.txt.
I tried to use ‘seek’ and ‘tell()’ but, to solve my problem above but, I got confused :(

I also tried this:

Open(FILE, “file1.txt”)
@theFile = <FILE>;

This puts every row in the array @the File. But, I can I now modify the elements of one row? (I’m still a novice Perl programmer)

Thank you for your help

/………………………………………………………………………………………../
The file portion

1 Chr26 1549367 11 GGGGGGGAAGA 8 3 Transition 11 0 -12.00 6.00 -0.25 -3.00 0.00 -1.67 -12.00 6.00 -0.64
1 Chr26 1549501 15 ccCctctccccctCC 12 3 Transition 3 12 -17.00 6.00 0.50 1.00 6.00 2.67 -17.00 6.00 0.93
1 Chr26 1549552 14 AagAAaaAAAagga 11 3 Transition 6 8 -31.00 6.00 -2.09 -12.00 3.00 -5.67 -31.00 6.00 -2.86
1 Chr26 1549563 14 tAAaaAAAattat^Ft 9 5 Transversion 5 9 -7.00 6.00 0.22 -64.00 4.00 -18.40 -64.00 6.00 -6.43
1 Chr26 1549726 14 TtTtctTtTtTTTT 13 1 Transition 8 6 -3.00 6.00 1.92 6.00 6.00 6.00 -3.00 6.00 2.21
2 Chr26 1549737 16 T+1Atttt+1aT+1At+1aTt+1aT+1AT+1AT+1AT+1AtT+1A^FA 15 11 Transversion 16 10 -64.00 6.00 -35.67 -64.00 6.00 -46.18 -64.00 6.00 -40.12
2 Chr26 1549815 9 CtCTTTTTT 7 2 Transition 8 1 -3.00 6.00 -0.14 -9.00 0.00 -4.50 -9.00 6.00 -1.11
1 Chr26 1549914 12 gGGGGGGGAGgg 11 1 Transition 9 3 -9.00 6.00 1.18 -4.00 -4.00 -4.00 -9.00 6.00 0.75
1 Chr26 1550018


Chris Charley
User

Oct 19, 2011, 8:51 AM

Post #2 of 16 (2252 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

Hello Herve,

The key to the solution is using a regular expression along with grep to filter out non-numerical expressions. To learn about grep, at your command prompt, type perldoc -f grep. There are 3 sections on regular expressions and you can read about them at:

perldoc perlre
perldoc perlretut
perldoc perlrequick

For your problem, the regular expression:
/^-?\d+(?:\.\d\d)?$/

can be explained:

^ start at the beginning of the string
-? an optional minus sign
\d+ one or more digits
(?:\.\d\d)? an optional (decimal point and 2 digits)
$ end of string



Code
 #!/usr/bin/perl  
use strict;
use warnings;

while (<DATA>) {
print join(" ", grep /^-?\d+(?:\.\d\d)?$/, split), "\n";
}

__DATA__
1 Chr26 1549367 11 GGGGGGGAAGA 8 3 Transition 11 0 -12.00 6.00 -0.25 -3.00 0.00 -1.67 -12.00 6.00 -0.64
1 Chr26 1549501 15 ccCctctccccctCC 12 3 Transition 3 12 -17.00 6.00 0.50 1.00 6.00 2.67 -17.00 6.00 0.93
1 Chr26 1549552 14 AagAAaaAAAagga 11 3 Transition 6 8 -31.00 6.00 -2.09 -12.00 3.00 -5.67 -31.00 6.00 -2.86
1 Chr26 1549563 14 tAAaaAAAattat^Ft 9 5 Transversion 5 9 -7.00 6.00 0.22 -64.00 4.00 -18.40 -64.00 6.00 -6.43
1 Chr26 1549726 14 TtTtctTtTtTTTT 13 1 Transition 8 6 -3.00 6.00 1.92 6.00 6.00 6.00 -3.00 6.00 2.21
2 Chr26 1549737 16 15 11 Transversion 16 10 -64.00 6.00 -35.67 -64.00 6.00 -46.18 -64.00 6.00 -40.12
2 Chr26 1549815 9 CtCTTTTTT 7 2 Transition 8 1 -3.00 6.00 -0.14 -9.00 0.00 -4.50 -9.00 6.00 -1.11
1 Chr26 1549914 12 gGGGGGGGAGgg 11 1 Transition 9 3 -9.00 6.00 1.18 -4.00 -4.00 -4.00 -9.00 6.00 0.75
1 Chr26 1550018

The split, (perldoc -f split), in this form splits $_ on one (or more) spaces. (also removes the newline at the end of $_).


(This post was edited by Chris Charley on Oct 20, 2011, 6:05 PM)


BillKSmith
Veteran

Oct 19, 2011, 8:52 AM

Post #3 of 16 (2251 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

The entire task can probably be done with a signle regular expression making clever use of the /g and /e options. I opted not to try because the following approach was easier to write and probably makes more sense to a C-programmer.


Code
use strict; 
use warnings;
open my $IN_HANDLE, '<', 'file1.txt' or die "Cannot open input file. $!";
open my $OUT_HANDLE, '>', 'file2.txt' or die "Cannot open output file. $!";
while (my $line = <$IN_HANDLE>) {
my @words = split /\s+/, $line;
my $line_out;
foreach my $word (@words) {
if ($word !~ m/[^-+.\d]/) {
$line_out .= $word . ' ';
}
}
print {$OUT_HANDLE} "$line_out\n";
}
close $IN_HANDLE;
close $OUT_HANDLE;



Note: A single space is used to separate the output fields regardless of how many may have been in the input. This is not a problem for the sample data.
Good Luck,
Bill


BillKSmith
Veteran

Oct 19, 2011, 10:49 AM

Post #4 of 16 (2240 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post

Charley,

Your idea to match real numbers suggest a better approach than either of us thought of. Use the RE from the common module to find the numbers. The search in list context with the \g option returns just the numbers. Including the whitespace in the RE preserves the original spacing. No split, grep, or join is required.


Code
use strict; 
use warnings;
use Regexp::Common;
use Readonly;
Readonly my $NUMBER => qr/$RE{num}{real}/;
open my $IN_HANDLE, '<', 'file1.txt' or die "Cannot open input file. $!";
open my $OUT_HANDLE, '>', 'file2.txt' or die "Cannot open output file. $!";
while (my $line = <$IN_HANDLE>) {
print $line =~ /$NUMBER\s+/g;
}
close $OUT_HANDLE;
close $IN_HANDLE;



The only known problem is that the newline is lost if the last field in a line is not a number.
Good Luck,
Bill


hervebags
Novice

Oct 19, 2011, 11:03 AM

Post #5 of 16 (2239 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

Hello Chris and BillSmith,

Thanks very much for your help.
I am unfortunately still having problems.

This is what I have, using BillSmith's help message:


Code
#!/usr/bin/perl -w 
require 5.10.1; ## The required version
use strict;

open FILE1, "<../file1.txt" or die "Cant not create the file!";
open FILE2, "+>file2.txt" or die "Cant not create file2.txt!";

while(my $line = <FILE1>)
{
my @words = split /\s+/,$line;
my $line_out;
foreach my $word (@words)
{
if ($word !~ m/[^-+.d]/)
{
$line_out .= $word . '';
}
}
print FILE2 "$line_out\n";
}
close FILE1;
close FILE2;



This is what i have, using Chris's help message:

Code
#!/usr/bin/perl -w 
require 5.10.1; ## The required version
use strict;

open FILE2, "+>file2.txt" or die "Cant not create file2.txt!";
my $line;

while($line = readline(ARGV))
{
print FILE2 $line;
print join(" ", grep /^-?\d+(?:\.\d\d)?$/, split), "\n";

}

close FILE2;


I get the same error with both code


Code
Use of uninitialized value $_ in split at extractColumns3.pl line 25, <> line 1. 

Use of uninitialized value $_ in split at extractColumns3.pl line 25, <> line 2.

Use of uninitialized value $_ in split at extractColumns3.pl line 25, <> line 3.

Use of uninitialized value $_ in split at extractColumns3.pl line 25, <> line 4.



Apologies for the basic questions. Im just a beginner.

Thank you for your help.

Herve


BillKSmith
Veteran

Oct 19, 2011, 5:00 PM

Post #6 of 16 (2223 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

You cannot get that message from your version of my code. (There is no $_). There are two typos in that code. The more serious is the missing backslash in the character class [^-+.\d]. (d matches itself, \d matches any digit from 0 through 9.) Fix this and the program will run, but there are no spaces between the fields in the output because of the second error. The space between the single quotes in the assingment to $line_out is missing. That space becomes the space between the words.

The pragma 'use warnings' is more flexible than the -w runtime option.

You should always use the three-argument form of open. (Refer perldoc -f open)

Always using the lexical (my variable) form of file handles rather than the bare-word form will save you a lot of trouble in the future.

Specifing the required version of perl is a good idea, but there is nothing in this code which requires version 5.10. I had to remove that line to run it on my version 5.8.

Did you look at my second post? Do you understand it?
Good Luck,
Bill


Chris Charley
User

Oct 19, 2011, 7:42 PM

Post #7 of 16 (2220 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

Herve



To make this code run, you have to change:

print join(" ", grep /^-?\d+(?:\.\d\d)?$/, split), "\n";

to

print FILE2 join(" ", grep /^-?\d+(?:\.\d\d)?$/, split ' ', $line), "\n";

(There's a space between the quote marks in split).

If I might make a few observations/suggestions (as BillSmith did).

open FILE2, "+>file2.txt" or die "Cant not create file2.txt!";

Do you know what happens when you use the +> notation. From the docs (perlfunc) for function open


Code
 You can put a + in front of the > or < to indicate that you want both read and  
write access to the file; thus +< is almost always preferred
for read/write updates--the +> mode would clobber the file first.
You cant usually use either read-write mode for updating textfiles, since
they have variable-length records.

So, I think what you want here is just the usual open for writing a new file.

open FILE2, ">", "file2.txt" or die "Cant not create file2.txt!";

(And as Bill said, you are better off using a lexical filehandle such as: open my $FILE2, ">", ...)

The line: while($line = readline(ARGV)) could be more simply written as: while (my $line = <>)

You are allowed to declare $line with my right in the while statement and you can get rid of the declaration of $line prior to the loop:

my $line;

close FILE2;
When closing a file, you should check that it closed correctly. You would write: close FILE2 or die "Unable to close ...";


(This post was edited by Chris Charley on Oct 20, 2011, 6:30 PM)


Chris Charley
User

Oct 19, 2011, 7:56 PM

Post #8 of 16 (2217 views)
Re: [BillKSmith] How to parse this file? [In reply to] Can't Post

Hello Bill


There is a problem in what is captured by the regex in your second solution. It picks up the '26' in 'Chr26' and the poster did not want to capture that. The only working solution I found, (for this data set anyway), is:


Readonly my $NUMBER => qr/(?<!\w)$RE{num}{real}/;


It works, but I'm not entirely comfortable with it. I've tried other prefixes to the regex but they haven't worked correctly.


(This post was edited by Chris Charley on Oct 19, 2011, 7:57 PM)


BillKSmith
Veteran

Oct 20, 2011, 7:04 AM

Post #9 of 16 (2206 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post

Good catch! Thanks.

I suspect that \S would be better than \w in the negative look-behind.
Good Luck,
Bill


Chris Charley
User

Oct 20, 2011, 6:28 PM

Post #10 of 16 (2181 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post

Herve

If by chance you read this post, I thought it might be helpful to explain how my code works. The main line:

Quote
print join(" ", grep /^-?\d+(?:\.\d\d)?$/, split), "\n";

Perl relies on the distinction of scalar vs. list context. Here, the join function expects a list (of values). This list is provided by grep which provides (a list) of numbers that matched the regular expression:

Quote
/^-?\d+(?:\.\d\d)?$/

grep in turn expects a list which split supplies. split in this form with no arguments splits the line read from the file. It is implicity contained in $_. By typing at the command line: perldoc -f split, you will gain a better understanding of split. split separates all the 'printable' groups of characters bounded by space(s).

It is helpful to view the entire statement as a stream of items to be processed starting from the right of the statement, (split), flowing to the left and (join).

You can begin a lot of action in a relatively short line of code in Perl.


hervebags
Novice

Oct 21, 2011, 12:36 PM

Post #11 of 16 (2167 views)
Re: [BillKSmith] How to parse this file? [In reply to] Can't Post

Hello BillkSmith,

Thank for your help and your tips so far.

I now have three problems. I will start with 2

1) BillkSmith, in your second answer you want me to use Regexp::Common and Readonly. When I do, I get these errors:

Can't locate rRegexp/Common.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at test.pl line 4.
BEGIN failed--compilation aborted at test.pl line 4.
I get this error when comment the line with Regexp::Common
Can't locate Readonly.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at test.pl line 5.
BEGIN failed--compilation aborted at test.pl line 5.


2) I tried to learn more about how to write regular expressions. I then tried to do get rid of the parenthesis and the commas in a file that looks like what I have pated below.

A portion of the file

(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)


I think I have followed all your goo-programming advices this time :).
This is what I have done :

Code
#!/usr/bin/perl 
use warnings;
use strict;

open my $FILE1, "<x.txt" or die "Can not open file1";
open my $FILE2, ">out.x" or die "Can not create file2";
while(my $line = $FILE1)
{
my @words = split /\d+/,$line;
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}/)
{
$line_out .= $word . ' ';
}
}
print $FILE2 "$line_out\n";
}

close $FILE1;
close $FILE2;

exit(0);
[\code]


hervebags
Novice

Oct 21, 2011, 12:36 PM

Post #12 of 16 (2167 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post

Hello Chris,

Thank for your help and your tips so far.

I now have three problems. I will start with 2

1) BillkSmith, in your second answer you want me to use Regexp::Common and Readonly. When I do, I get these errors:

Can't locate rRegexp/Common.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at test.pl line 4.
BEGIN failed--compilation aborted at test.pl line 4.
I get this error when comment the line with Regexp::Common
Can't locate Readonly.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at test.pl line 5.
BEGIN failed--compilation aborted at test.pl line 5.


2) I tried to learn more about how to write regular expressions. I then tried to do get rid of the parenthesis and the commas in a file that looks like what I have pated below.

A portion of the file

(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)


I think I have followed all your goo-programming advices this time :).
This is what I have done :

Code
#!/usr/bin/perl 
use warnings;
use strict;

open my $FILE1, "<x.txt" or die "Can not open file1";
open my $FILE2, ">out.x" or die "Can not create file2";
while(my $line = $FILE1)
{
my @words = split /\d+/,$line;
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}/)
{
$line_out .= $word . ' ';
}
}
print $FILE2 "$line_out\n";
}

close $FILE1;
close $FILE2;

exit(0);
[\code]


BillKSmith
Veteran

Oct 22, 2011, 8:14 AM

Post #13 of 16 (2145 views)
Re: [hervebags] How to parse this file? [In reply to] Can't Post

You must install the modules. ActiveState perl users should use the utility ppm. All others should use the CPAN web site. (Perhaps someone else will give you more detail on how to do this.)



Regarding your regular expression:

It is better to use the common module. It is well debugged. I bet that there are special cases that neither of us has thought of.

Use a character class rather than alternation. [-+]? matches a sign character if present.

The period has special meaning in a regular expression. You must escape it with a backslash to make it match itself.

Use /d rather than (0|1|2...). it is faster and does not have to create a back-reference.

I prefer * rather than {0,}. No real reason.

Use (?:E[-+]?\d{3})? to match the exponent. (?:.....) groups like (.....), but does not create a back-reference. E matches itself. [-+] matches the sign. \d{3} requires three digits. The final ? makes the entire exponent optional.



There are still a few programming style issues with your open statements.:

Use the three argument form.

Use single quotes unless double quotes are needed for interpolation.

Except in special cases, you should include $! in your error message.
Good Luck,
Bill


Chris Charley
User

Oct 22, 2011, 10:07 AM

Post #14 of 16 (2139 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.

Code
     #!/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.

Code
     #!/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.

Quote
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)


BillKSmith
Veteran

Oct 22, 2011, 1:51 PM

Post #15 of 16 (2120 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post


Quote

Code
  s/^\(//; # lose the opening parenthesis      
s/\)$//; # lose the closing parenthesis
s/,/ /; # substitute a space for the comma





This is a job for the translation operator.


Code
tr/,()/ /d;

Good Luck,
Bill


hervebags
Novice

Oct 23, 2011, 5:21 PM

Post #16 of 16 (2083 views)
Re: [Chris Charley] How to parse this file? [In reply to] Can't Post

Wow! You wrote all the code in just few lines!
Thanks for your help and tips :)
I can not find ''Switch to Basic Editor' at the bottom of the posting window, though. There is only: "preview Post", "check Spelling" and "Post Reply"

Herve

 
 


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

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