• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Perl Hash

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
SheldonC
Asked:
SheldonC
  • 7
  • 5
  • 4
  • +1
1 Solution
 
ozoCommented:
see
perldoc -q duplicate
0
 
SheldonCAuthor Commented:
thanks for your response but isn't there another way to do it using hashes?
0
 
ozoCommented:
there are several ways, of which perldoc mentions a few
are you interested in any particular way?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
SheldonCAuthor Commented:
yes, using hash
0
 
rajeeshbCommented:
#!/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
 
wilcoxonCommented:
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
 
ozoCommented:
most of the ways recommended by perldoc use hash
what other way did you want?
0
 
ozoCommented:
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
 
SheldonCAuthor Commented:
#!/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
 
wilcoxonCommented:
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
 
ozoCommented:
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
 
SheldonCAuthor Commented:
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
 
ozoCommented:
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
 
wilcoxonCommented:
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
 
SheldonCAuthor Commented:
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
 
wilcoxonCommented:
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
 
ozoCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now