Hi there everyone, I made my first contribution to an open source project today. Well, to be honest, the project was just a list of links and the contribution was just a script that most of you could have hacked together in 10 minutes... But I felt really good about it :D.
Now, as I am pretty new to Perl (and just started programming this year) and I'm sure that my code is pretty clunky because of that, I figured that it might be a good idea to ask for some comments on the first 30+ line program I have ever written in Perl. Are my comments on point for example? Should I comment more/less? And could my code have been shorter? Please feel free to criticize every single character and white space in my script! It won't hurt me (much).
First I'll give you an idea of the way the files are formatted; The <> tags are not in the real files!
# <CATEGORY HEADER> [<name of resource>] (www.hyperlink.com) some text about the resource [<name of resource>] (www.hyperlink.com) some text about the resource [<name of resource>] (www.hyperlink.com) some text about the resource #<HEADER OF NEXT CATEGORY> [<name of resource>] (www.hyperlink.com) some text about the resource [<name of resource>] (www.hyperlink.com) some text about the resource
#!/usr/bin/env perl # Sort all entrys in a list of links in this repo (https://github.com/ +TumblrCommunity/coding-masterpost/tree/PythonNerd-patch-1) # Author: redrock9 (https://github.com/redrock9) # This script modifies files. The modifications are permanent, but a b +ackup with a ".backup" extention will # be made in the directory in which the file is located before any mod +ifications take place. # How to use: execute this script with the path to a file as argument, + for example: ./sort.plx Resources/test.md use strict; use warnings; use File::Copy qw(move); # Take filehandle as agrument my $file = $ARGV[0]; # Open file and put line sin an array open (TEXT, "< $file") or die "Can't open $file for read: $!"; my @lines = <TEXT>; map {s/\s+$//} @lines; close TEXT or die "Cannot close $file: $!"; # get keys for each line and store each line in a hash my @keys; my %linesToSort; my $i = -1; # Switches between categories (increments when a line star +ting with # is encountered) my $a = 1; # in this loop: number to append to keys that are otherwise + double in the hash for (0..$#lines) { if ($lines[$_] =~ /^\#(?:.*)/){ $i++ } elsif ($lines[$_] =~ /(?:^\[)(.*?)(?:\])/){ my($key) = $1; if ($linesToSort{$key}){ $linesToSort{"${key}$a"} = "$lines[$_]"; push @{$keys[$i]}, "${key}$a"; $a++; } else { $linesToSort{$key} = "$lines[$_]"; push @{$keys[$i]}, "$key"; } } } # Print the number of lines in the file before modification print "the number of lines before sorting is: $#lines\n"; # Sort keys lexicographically map {@{$_} = sort @{$_}} @keys; # Make a backup of the file move("$file", "${file}.backup"); # Print the sorted lines into the file $i = -1; # Switches between categories (increments when a line startin +g with # is encountered) $a = 0; # In this loop: The amount of lines per category (is set to 0 +every time a # is encountered) open (NEWTEXT, "> $file") or die "Can't open $file for read: $!"; for (0..$#lines) { if ($lines[$_] =~ /^\#(?:.*)/){ print NEWTEXT "$lines[$_]\n"; $i++; $a = 0; } elsif ($lines[$_] !~ /(?:^\[)(.*?)(?:\])/){ print NEWTEXT "$lines[$_]\n" } elsif($keys[$i][$a]){ print NEWTEXT "$linesToSort{$keys[$i][$a]}\n"; print "$keys[$i][$a]\n"; $a++; } } close NEWTEXT or die "Cannot close file $file: $!"; print "-----SORTING COMPLETED-----\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: My first GitHub contribution
by Your Mother (Archbishop) on Mar 05, 2017 at 23:45 UTC |