Link to home
Start Free TrialLog in
Avatar of magento
magento

asked on

Need perl help

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
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of magento
magento

ASKER

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
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].
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).