Solved

Perl Hash

Posted on 2011-09-22
17
382 Views
Last Modified: 2012-05-12
How can I print to the screen duplicate strings from an array using hash

the array has just one element but it is alphanumeric (example Hod524).


my array looks like this:
@username=($fi,$mi,$li,$positionlast);
where $fi,mi,and li,position last are substring of another array.

What I am trying to accomplish is to use the array @username and print out all the duplicates in the username array.






thanks
0
Comment
Question by:SheldonC
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 36584167
see
perldoc -q duplicate
0
 

Author Comment

by:SheldonC
ID: 36584200
thanks for your response but isn't there another way to do it using hashes?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36584422
there are several ways, of which perldoc mentions a few
are you interested in any particular way?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SheldonC
ID: 36584794
yes, using hash
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36586387
#!/usr/bin/perl

my @array = qw( a a b c c c d e f e f e a f g h h h );
my @unique = ();
my %seen = ();
foreach my $elem ( @array )
{
    (print "found duplicate entry of : $elem\n" and next) if $seen{ $elem }++;
    push @unique, $elem;
}

print "@unique\n";
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36587013
Or another alternative.  rajeshb's %seen method is more efficient and common but I thought I'd throw out another variant.
my %cnt;
foreach (@username) { $cnt{$_}++ }
print "duplicate usernames:\n", map { "$_\t$cnt{$_}\n" } grep { $cnt{$_} > 1 } keys %cnt;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 36590581
most of the ways recommended by perldoc use hash
what other way did you want?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36590670
The most natural ways to find duplicate strings from an array using hash
are the ones covered by perldoc or variants thereof,
But if you want another way, here's one not mentioned in
perldoc -q duplicate
(and likely nowhere else in perldoc either)


@array=( 1, 2, 3, 4, 4, 5, 6, 5, 7 );
@h0{@array}=(0..@array);
@h1{reverse @array}=(1..@array);
$\=$/;
print for grep{$h0{$_}+$h1{$_}!=@array}keys %h0;
0
 

Author Comment

by:SheldonC
ID: 36601031
#!/usr/bin/perl

my @array = qw( a a b c c c d e f e f e a f g h h h );
my @unique = ();
my %seen = ();
foreach my $elem ( @array )
{
    (print "found duplicate entry of : $elem\n" and next) if $seen{ $elem }++;
    push @unique, $elem;
}

print "@unique\n";

Open in new window


this is similar to what I am looking for but I would like to print out the duplicate strings for each line in a file by passing a string into a variable.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36601078
Are you saying you want to compare entire lines in a file, all words on each line independently within a file, or all words within a file globally?  I'm also not sure what you mean by "by passing a string into a variable"?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36601372
you originally asked for duplicate strings from an array
now you are talking about duplicate strings for each line
do you need to break a line into separate strings and put the strings into an array?
or do you want to make each line a separate array element?
if so, you can do that with something like
@array = <FILE>;
if you want to break a line apart into separate strings,
you can probably use split or m// depending on how you want to separate the strings
0
 

Author Comment

by:SheldonC
ID: 36601401
let me start over. I extracted a wordsfrom a file using substr
I passed this word into an array called @username

I want to find all the duplicate words in the array @username and print out the duplicates

example
adg
hgf
dgh
adg


I want it to print adg found


0
 
LVL 84

Expert Comment

by:ozo
ID: 36601421
I am also puzzled by your statement
the array has just one element
A unique element would not be a duplicate element.
Also, what is the purpose of using an array if it has just one element?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36601598
I'm still not understanding exactly what you want.  Let me try asking some questions.

What, if anything, do you want to pass into the script on the command line?

What files, if any, should the script read from?

Do you want each duplicate output in the order found, each duplicate output only once, duplicates output with a count of occurrences, or something else?

In short, exactly what (and in what form) should the input look like and where should it come from and what exactly should the output look like?  A sample input (command line and/or file) and output would be helpful in providing a script for you.
0
 

Author Comment

by:SheldonC
ID: 36601691
I extracted from each line in a file the first inital of the first name, the first initial of the middle name and the first initial of the last name
example : John T Jones (JTJ)
I do this for each line in the file
so the output for each line in the file may look like below:
JTJ
ASD
GFY
JTJ

I want to be able to print JTJ is a duplicate entry

         
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 36601850
If I'm understanding what you want, this should work...
#!/usr/bin/perl
use strict;
use warnings;

my @username = qw( JTJ ASD GFY JTJ );
my %seen;
foreach my $user ( @username ) {
    print "$user is a duplicate\n" if $seen{$user};
    $seen{$user}++;
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 36707674
if you have qw(
JTJ
ASD
GFY
JTJ
)
in an array, then any of the suggestions in perldoc should do what you want
why do you ask for another way?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

749 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