Solved

Need perl help

Posted on 2014-01-04
5
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 450 total points
ID: 39756178
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
ID: 39756197
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
ID: 39756254
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
ID: 39756516
$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
ID: 39756536
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

632 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