Sagar S said:
I am a newbie to Perl and now I am stuck with a problem (which may seem
simple to all the Perl Gurus out there ;-) )
Please see the Posting Guidelines that are posted here frequently.
I am having a list like (XYZ_A(8::7), XYZ_B(4,15), XYZ_C(4,6), XYZ_A(8,9),
XYZ_A(6,15), XYZ_B(8,10) ) for example.
That is not a Perl list.
Please post Real Perl Code.
The problem is I have to retain only one element of each kind (XYZ_A, XYZ_B,
XYZ_C etc)
Since your problem does not seem to be how to get such a list in
Perl code, then you should have been able to post it in Perl code.
such that for each kind, only those which have the lowest first
number are retained. I mean, after processing, the list should be
(XYZ_B(4,15), XYZ_C(4,6), XYZ_A(6,15))
If they must be in that order, then the problem gets a good bit harder...
I will really appreciate any help in this direction,
Please indicate what you need help with, and what part you already
know how to do.
The best and easiest way to show the parts that you already know how
to do is to post the Perl code that does it.
Do we need to help you create the list?
Do we need to help you extract the 2 "interesting" parts from
each list item?
Do we need to help you track which one is the least for each "name"?
Do we need to help you with the print() statement for making the
final output?
You will be much more likely to get a little help rather than get
a lot of help.
A blanket "help me" makes the task of helping so large that many
or most potential answers will just move on to helping the next
poster instead.
I just happen to have more time than usual today, or I would have
done that too.
probably a code snippet
or some hints atleast.
This is a much too intricate problem for an early Perl programmer
to tackle. You'll need to learn many different things before you'll
be able to do it.
Choose a simpler problem first, move to more complex problems after
you have at least a bit of experience.
The primare elements of solving your problem are to use a m// in
a list context to pluck out the 2 interesting parts, and a
multi-level hash to record each of the 3 parts.
Multi-dimensional data structures are not a "beginning" topic...
--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
my @array = ( 'XYZ_A(8::7)',
'XYZ_B(4,15)',
'XYZ_C(4,6)',
'XYZ_A(8,9)',
'XYZ_A(6,15)',
'XYZ_B(8,10)',
);
my %least; # keep track of the least number for each
foreach my $item ( @array ) {
my($name, $num) = $item =~ /([^(]+) # up 'til the 1st paren goes in $1
\( # opening paren
(\d+) # some digits go in $2
/x;
if ( !exists $least{ $name } ) { # 1st time we've seen this one
$least{ $name }{ num } = $num;
$least{ $name }{ item } = $item;
next; # done recording this one
}
if ( $num < $least{ $name }{ num } ) { # need to update
$least{ $name }{ num } = $num;
$least{ $name }{ item } = $item;
}
}
print "$least{ $_ }{ item }\n" for sort keys %least;