Solved

PERL format

Posted on 2009-07-06
3
142 Views
Last Modified: 2012-05-07
I have string $str = "20090630|A|0.5
                                 20090630|B|0.7";
basically a file wiht 2 lines is read in to a string.
I want to output string in the format 20090630,0.5,0.7
Can this be done in one liner ?
I know we can split with "\n" first and then with "|" but i want to know if it can be done in one line ?

Thanks for your help
0
Comment
Question by:koppcha
3 Comments
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 50 total points
ID: 24791366
my $str = "20090630|A|0.5
        20090630|B|0.7";

$str =~ /([^|]+)\|([^|]+)\|([^|\s]+)\s+([^|]+)\|([^|]+)\|([^|]+)/m;
print "$1,$3,$6\n";
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 200 total points
ID: 24791376

my $str = "20090630|A|0.5
           20090630|B|0.7";
 
print "$1,$2,$3\n" if $str =~ /(\d+)\|\w+\|([\d\.]+).*\|([\d\.]+)$/s;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24791668
my $str = "20090630|A|0.5
        20090630|B|0.7";
print join",",split/(?:\s+\d+)?\|\w+\|/,"$str\n";
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 104
perl split by | 2 103
Perl Script - Remove column of data based on column value 2 53
How to search multiple patterms in a file with perl? 4 82
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question