CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Flagging list data hybrid situation: Edit Log



stuckinarut
User

Aug 22, 2015, 11:28 PM


Views: 2332
Flagging list data hybrid situation

I wasn't sure exactly how to describe this in terms of a 'Subject' line.

This is a Step 1 and Step 2 situation.

STEP 1
=====
To end up with a sorted list based upon the numerical count of identical entries (using fruit names in this short list an example):


Code
apple 
grape
grape
peach
peach
peach
peach
pear
pear
pear


This code *partially* solves the problem:


Code
use strict; 
use warnings;

my %map;
my @sorted;
chomp(my @words = <STDIN>);

foreach my $word (@words) {
$map{$word} += 1;
}

@sorted = reverse sort { $a cmp $b } @words;

foreach my $key (sort keys %map) {
print "$key $map{$key} \n";
}


I end up with this:


Code
apple 1  
grape 2
peach 4
pear 3


The desired output is for the data to be 'descending' numerical order, but I can't figure out the correct sorting code needed:


Code
peach 4 
pear 3
grape 2
apple 1


STEP 2
====

NOTE: An objective is also to hopefully eliminate the need to manually type in all the data via <STDIN>, and simply use the initial list (in STEP 1) with all duplicate entries as 'listL.txt" in the following code which I am 're-purposing' from a previous task.


Code
use strict;   
use warnings;

my %D_list;
my %I_list;

open my $D_list, '<', 'listD.txt' or die "Cannot open listD.txt: $!";
while (my $line = <$D_list>) {
chomp $line;
$line =~ s/\r//g; # Removes Windows CR characters
$line =~ s/\s+$//; # Removes trailing white spaces
$D_list{$line} = 1
}
close $D_list;

my %I_list;
open my $I_list, '<', 'listI.txt' or die "Cannot open listI.txt: $!";
while (my $line = <$I_list>) {
$line =~ s/\r//g;
$line =~ s/\s+$//;
chomp $line;
$I_list{$line} = 1
}
close $I_list;

my ($L_count, $D_count, $I_count);
open my $L_list, '<', 'listL.txt' or die "Cannot open listL.txt: $!";
while (<$L_list>) {
chomp;
s/\r//;
s/\s+$//;
$L_count ++;
print;
$D_count ++ and print ' D' if exists $D_list{$_};
$I_count ++ and print ' I' if exists $I_list{$_};
print "\n";
}
print "L: $L_count; D: $D_count; I: $I_count; \n";


The 'listD.txt' sample entries are:


Code
grape 
pear


The 'listI.txt' sample entries are:


Code
apple 
grape
peach
pear


So the desired Final-Final output would look like this - with any 'listL.txt' entries FLAGGED if they also appear as entries on 'listD.txt' or 'listI.txt":


Code
peach 4 I 
pear 3 D I
grape 2 D I
apple 1 I


In order to match ONLY the fruit names on 'listL.txt' this REGEXP seems to make sense, but I got stuck trying to figure out exactly how to use it ;-(


Code
# Match ONLY to fruit name without the number 
if ($L_list =~ m/^([\w]+)\s) {

- not sure how to do this -

}


Thanks for any assistance!

-Stuckinarut


(This post was edited by stuckinarut on Aug 23, 2015, 1:12 PM)


Edit Log:
Post edited by stuckinarut (User) on Aug 22, 2015, 11:48 PM
Post edited by stuckinarut (User) on Aug 23, 2015, 8:06 AM
Post edited by stuckinarut (User) on Aug 23, 2015, 8:20 AM
Post edited by stuckinarut (User) on Aug 23, 2015, 1:12 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives