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: Beginner: Re: [Raju_P] Program to filter and remove duplicates: Edit Log



Laurent_R
Veteran / Moderator

Feb 2, 2013, 11:04 AM


Views: 377
Re: [Raju_P] Program to filter and remove duplicates

Did you start to write something?

Assuming you know how to open and read a file line by line, you could use something like this to keep only the cities:


Code
while (my $line = <$input_file>) { 
chomp $line;
next unless $line =~ /City\s*$/; # discards lines which are not cities
my @temp_array = split /\s*:/; # split the line into an array
my $city = shift @temp-array; # get the city name
# now try to find duplicates
}


For removing duplicates, the idiomatic way is to use a hash. You check if the city name exists in the hash; if it does exist, it is a duplicate; if not, add it to to the hash and print the line out.


(This post was edited by Laurent_R on Feb 3, 2013, 2:21 AM)


Edit Log:
Post edited by Laurent_R (Veteran) on Feb 3, 2013, 2:21 AM: Removed a typo


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

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