Solved

PERL format

Posted on 2009-07-06
3
139 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
Comment Utility
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
Comment Utility

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
Comment Utility
my $str = "20090630|A|0.5
        20090630|B|0.7";
print join",",split/(?:\s+\d+)?\|\w+\|/,"$str\n";
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now