Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Sorting unique values in file using perl

by 2teez (Priest)
on Oct 24, 2012 at 02:55 UTC ( #1000543=note: print w/replies, xml ) Need Help??

in reply to Sorting unique values in file using perl

Please also check this How do I post a question effectively?.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
  • Comment on Re: Sorting unique values in file using perl

Replies are listed 'Best First'.
Re^2: Sorting unique values in file using perl
by PerlSavvy (Initiate) on Oct 24, 2012 at 03:08 UTC
    my $file1="file1.txt"; open FILE1, "<$file1" or die $!; my $file2="min_viols_endpointSorted.csv"; open FILE2, ">$file2" or die $!; while(<FILE1>){ my $path = $_; $path =~ /([^\s]+)/; $path = $1; #Extracting path chop($path); my $slack = $_; $slack =~ /[^\f+][\s+][\f+][\s+][\f+][\s+]([\f+]+)[\s](VIOLATED)/; $slack = $1; print "$slack\n"; chop($slack); print FILE2 "$path $slack\n"; }

    After this I plan to read the csv into a hash and compare the values of hash keys. If one hash value for a duplicate key is less than previous value than put it in a output file. And output should be unique, as I have shown in my question. Please help on this.

      $path =~ /([^\s]+)/; $path = $1; #Extracting path ... $slack =~ /[^\f+][\s+][\f+][\s+][\f+][\s+]([\f+]+)[\s](VIOLATED)/; $slack = $1;

      You shouldn't use the results of a regular expression unless you verify that the pattern matched or you may get erroneous results.    Also /([^\s]+)/ is usually written as /(\S+)/ and /[^\f+][\s+][\f+][\s+][\f+][\s+]([\f+]+)[\s](VIOLATED)/ matches a single character that is not a FORM FEED or '+' character, followed by a whitespace or '+' character, followed by a FORM FEED or '+' character, etc., but there are no FORM FEED characters in the string.

      chop($path); ... chop($slack);

      chop removes the last character of the string, no matter what it is.    So what is the purpose of removing the last character from $path or $slack?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000543]
[Corion]: I found plenty of not chatty enough logs with Perl too ... I'm slowly coming to appreciate Log::Log4perl resp. our homegrown alternative
[tye]: my experience with python so far is more like finding a moderately useful error message is a shocking surprise.
Corion disappears
[tye]: I get an error string but no reason and no context. Like die "Could not read file.\n" but with a stack trace where all of the useful context is stripped.
[thezip]: Heya tye!
[tye]: hey, thezip!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2017-09-21 20:18 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (252 votes). Check out past polls.