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

x
?
Solved

Need perl help

Posted on 2014-01-04
5
Medium Priority
?
276 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 27

Accepted Solution

by:
wilcoxon earned 1800 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 27

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 85

Assisted Solution

by:ozo
ozo earned 200 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 27

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Six Sigma Control Plans

885 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