Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PERL format

Posted on 2009-07-06
3
Medium Priority
?
149 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 200 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 800 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 85

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

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