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
Solved

Need perl help

Posted on 2014-01-04
5
254 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
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

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

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 120
Reading fields from the text file. 4 101
Merge files & delete row based on criteria using Perl. 1 104
compress files in RAR using perl 13 83
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…
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…

809 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