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:
String manipulation in perl: Arranging data

 



dkhanna01
Novice

Jan 12, 2005, 12:46 AM

Post #1 of 2 (268 views)
String manipulation in perl: Arranging data Can't Post

I have my data like this

"cr01,cr02":file1

"cr01":file2

"cr02":file3

"cr01,cr03":file4

"cr03":file5

Now I want my output to be printed as:

"cr01":

file1

file2

file4

"cr02":

file1

file3

file5

"cr03":

file4

file6

Kindly let me know how can I do it in perl


davorg
Thaumaturge / Moderator

Jan 13, 2005, 5:22 AM

Post #2 of 2 (262 views)
Re: [dkhanna01] String manipulation in perl: Arranging data [In reply to] Can't Post

This looks like it does what you want. The output isn't exactly the same as yours, but I think your sample output wasn't created from your samlpe input.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my %data;

while (<DATA>) {
next unless /\S/;

chomp;

my ($keys, $file) = split /:/;

$keys =~ s/^"//;
$keys =~ s/"$//;

my @keys = split /,/, $keys;

foreach my $key (@keys) {
push @{$data{$key}}, $file;
}
}

foreach my $key (sort keys %data) {
print "$key\n\n";

print map { "$_\n\n" } @{$data{$key}};
}

__DATA__
"cr01,cr02":file1

"cr01":file2

"cr02":file3

"cr01,cr03":file4

"cr03":file5


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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