Solved

Perl Hash

Posted on 2011-09-22
17
350 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
Comment Utility
see
perldoc -q duplicate
0
 

Author Comment

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

Expert Comment

by:ozo
Comment Utility
there are several ways, of which perldoc mentions a few
are you interested in any particular way?
0
 

Author Comment

by:SheldonC
Comment Utility
yes, using hash
0
 
LVL 1

Expert Comment

by:rajeeshb
Comment Utility
#!/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
Comment Utility
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
Comment Utility
most of the ways recommended by perldoc use hash
what other way did you want?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:SheldonC
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now