Solved

Need perl help

Posted on 2014-01-04
5
246 Views
Last Modified: 2014-01-05
Hi ,
I am learning perl and i cant able to find the logic to get it worked.

Input
1,AC10308120AC1030812A.jpg
1,AC10308120AC1030812B.JPG
1,AC1030812AC1030812A.jpg
2,BA037BA037.jpg
2,BBF03BBF03A.jpg
3,BBYF03BBYF03A.jpg

Output needed:
1,AC10308120AC1030812A.jpg,AC10308120AC1030812B.JPG,AC1030812AC1030812A.jpg
2,BA037BA037.jpg,BBF03BBF03A.jpg
3,BBYF03BBYF03A.jpg
0
Comment
Question by:magento
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 450 total points
Comment Utility
use strict;
use warnings;
my %data;
while (<>) {
    chomp;
    my ($num, $fil) = split /,/;
    $data{$num} = [] unless exists($data{$num});
    push @{$data{$num}}, $fil;
}
foreach my $num (sort { $a <=> $b } keys %data) {
    print "$num,", join(',', @{$data{$num}}), "\n";
}

Open in new window

0
 
LVL 5

Author Comment

by:magento
Comment Utility
Hi Wilcoxon,

Works like charm ..

Can you please add few comments on the below so i can learn it.


my ($num, $fil) = split ",";

So the above line will split using comma and place the key value respectively
so i get $num=1 and $fil=image1.jpg for 1st line

push @{$data{$num}}, $fil;

so here we push the $fil to the array and we have

@array = (1,image.jpg);

Am i right, i dont understand this part , please clarify

Thanks
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
split uses a regex (hence /,/ instead of ',').  Yes, split will split the value ($_ by default) using the regex provided into a list.  Since there are only two values per line, I prefer to enumerate each (the general form would be @list = split /,/).

Not quite.  %data is a hash so $data{$num} is an element of the hash (in this case $data{1}).  Push adds the value ($fil) to the array (which in this case is really the arrayref $data{1}).  So what we really have is $data{1} = [image.jpg].
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
Comment Utility
$data{$num} = [] unless exists($data{$num}); # is uneccessary.  It would be autovivified by the push

split ","; # is the same as split /,/;  # although split " "; is not the same as split / /;
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
I never remember which actions autovivify so I usually default on the safe side.

The differences in behavior for some values is why I always use regex syntax for split (plus that's what the perldoc suggests for syntax).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

11 Experts available now in Live!

Get 1:1 Help Now