LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2012, 10:42 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
perl duplicates hash array


SO I have a file - lets call it coocoo

GRNB is missing a regsho on capser0001
SAN-F is missing a regsho on capser0001
PSA-V is missing a regsho on capser0001
BK-C is missing a regsho on capser0001
GRNB is missing a regsho on capser0040
SAN-F is missing a regsho on capser0040
PSA-V is missing a regsho on capser0040
BK-C is missing a regsho on capser0040


All i want is the first column (but don't want to use awk)

so i make a script in perl

Code:
#!/usr/bin/perl
my $column; 
my $first_column

open REGSHO, "/tmp/coocoo" or die $!;
while (<REGSHO>) {
        chomp $_ ;
        my @column = split /\s/, $_;
        push @first_column , $column[0];


}
foreach my $elem (@first_column )  {
                print "$elem\n"
}
which gives me this

Code:
GRNB
SAN-F
PSA-V
BK-C
GRNB
SAN-F
PSA-V
BK-C
YAY!!!!!!!!!!!!

However I want to keep this unique - so i put i a hash to get rid of the dupes.
Code:
#!/usr/bin/perl
my %seen;
my $column; 
my $first_column

open REGSHO, "/tmp/coocoo" or die $!;
while (<REGSHO>) {
        chomp $_ ;
        my @column = split /\s/, $_;
        push @first_column , $column[0];


}

foreach my $elem (@first_column )  {
        next if $seen{ $elem }++;
        push @first, $elem;
}

foreach my $first (@first) {
        print $first;
        print "\n" ;
}
which gives me this --- BOOOOOO!!!!

Code:
GRNB
SAN-F
PSA-V
BK-C
GRNB
how do i get rid of the first duplicate GRNB ?

this is so, so easy in bash.
 
Old 09-24-2012, 11:55 PM   #2
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Why don't you want to use Awk?

Code:
awk '{print $1}' coocoo.txt | awk '!a[$0]++'

Last edited by amboxer21; 09-25-2012 at 12:33 AM.
 
Old 09-25-2012, 01:16 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
in perl you need to insert -w in the first line, and also you need to add use strict, so your script should begin with:
Code:
#!/usr/bin/perl -w
use strict;

....
You will find a lot of problems, like:
my $first_column is not used at all, there is no ; at the end of this line (there are other problems too). You introduced a hash, so use it:
Code:
open REGSHO, "/tmp/coocoo" or die $!;
while (<REGSHO>) {
        chomp $_ ;
        my @column = split /\s/, $_;
        $seen{$column[0]} = 1;
}
close REGSHO or die $!;

# set separator
my $, = "\n";
print keys %seen;
(not tested)
 
Old 09-25-2012, 06:19 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
And if we were really going to use awk, definitely no need to call it twice:
Code:
awk '!_[$1]++{print $1}' file
 
1 members found this post helpful.
Old 09-25-2012, 03:14 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi,

with Perl you don't even need an array,
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my %seen ;

open REGSHO, "./coocoo" or die $!;
while (<REGSHO>) {
        $seen{ (split /\s/, $_ )[0] } = 1; # split is always in list-context and can therefore be used as an array
}
close REGSHO ;

# set separator
$, = "\n";
print keys %seen;
print "\n" ;
also you don't need chomp, chomp removes the linefeed at the end of a line, but you're only dealing with the first column (which of course has no linefeed).

As pan64 wrote, use strict and warnings!

Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl array nof hash zahheb Linux - Newbie 5 08-27-2012 03:12 AM
Perl Hashes -- Updating a hash ref via hash value 0.o Programming 5 06-05-2012 12:45 PM
Perl Hash of Hash reference query kdelover Programming 1 02-19-2011 04:47 AM
[SOLVED] Perl - How to store and access an array in a hash of hashes? DevonB Programming 4 07-07-2010 07:57 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration