Post whatever code you have tried so far even if it does not work. That way people can at least get a sense about how much perl understanding you do have. Only posting your programming requirements and showing no effort makes it look like you just want someone to write your code for you, and this has the ring of school work to it.
You should use "strict" and "warnings" when writing perl scripts, especially for new perl programmers. But anyway..... the code you posted should be returning an error (or errors). What is the error you get?
from the code that you said should be putting out an error, these are the errors produced
Scalar found where operator expected at ./counter.pl line 7, near "$lat$lon"
(Missing operator before $lon?)
Scalar found where operator expected at ./counter.pl line 8, near "$lat$lon"
(Missing operator before $lon?)
syntax error at ./counter.pl line 7, near "$lat$lon"
Execution of ./counter.pl aborted due to compilation errors.
Perl is trying to help you but it can't always tell exaclty what the error is or even exactly where it is. But in this case its sort of staring you inthe face:
near "$lat$lon"
in fact that is exactly the error, you have no double-quotes around $let$lon so that perl can combine them into a string and use them as a hash key. So lets fix that and move on. I am going to add three very useful pragmas to your script: strict, warnings, diagnostics
Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my %hash;
open (MYFILE, "All_origins.txt");
while (<MYFILE>) {
my ($lat, $lon) = split(/\s*/);
if (exists $hash{"$lat$lon"}) {
$hash{"$lat$lon"} .= ",$lat$lon";
next;
}
$hash{"$latlon"} = "$latlon";
}
for (keys %hash) { print "$_ $hash{$_}\n"; }
exit;
close (MYFILE);
Run that and see what happens. Note that I fixed another error in your code, see if you can find it.
Last edited by perl_diver; 06-04-2009 at 06:19 PM.
Reason: correct spelling
I can only assume you did not run the code I posted because you should have gotten another error. I was hoping you would pick up the error (the one I did not correct). You did find the one I corrected: \\s*. So if you did not run the code I posted, what did you run?
anyway... here is the problem with the last code I posted. THsi line taken from your original code:
Code:
$hash{"$latlon"} = "$latlon";
There is no $ symbol before 'lon', should be:
Code:
$hash{"$lat$lon"} = "$lat$lon";
Otherwise you will get an error about $latlon not being packaged. Also, while I did correct \\s* in split, your real split pattern should be \s+ whcih means one or more spaces instead of zero or more spaces. But all this is really academic, which was the point since you are learning perl. If all you wanted to do was count duplicate lines in the file your code is too verbose, this will siffice:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my %hash;
open (MYFILE, "All_origins.txt") or die "$!";
while (<MYFILE>) {
chomp;
$hash{$_}++;
}
close (MYFILE);
for (sort {$hash{$b} <=> $hash{$b} } keys %hash) {
print "$_ ($hash{$_})\n";
}
exit;
The splitting of the line into two tokens is not necessary unless there is a variable number of spaces between the lat and the lon. And of course your code wasn't even counting anything but I assume you were going to get to that after getting your initial code to run.
i dont think that $lat$lon is going to get what i want, i want to match the lat lon string to any other lat lon string that is identical but the lat lon strings are
50.000 240.000
so there is a space between the two. $lat$lon doesnt do that. When i run the code now, i recieve no errors. but the output puts out something like
so it appears that it is correctly identifying the matching lats but not lons and not outputting the correct thing.
By the way this is not a hw assignment. This is for my graduate research, i normally work in IDL but when i need to do some regular expressions or text manipulation i try to use perl or shell scripts. But i havent learned much perl.
Here is the code as i am running it now
Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my %hash;
open (MYFILE, "All_origins.txt");
while (<MYFILE>) {
my ($lat, $lon) = split(/\s*/);
if (exists $hash{"$lat$lon"}) {
$hash{"$lat$lon"} .= ",$lat$lon";
next;
}
$hash{"$lat$lon"} = "$lat$lon";
}
for (keys %hash) { print "$_ $hash{$_}\n"; }
exit;
close (MYFILE);
Bookmarks