Solved

PERL format

Posted on 2009-07-06
3
140 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now