my %path; my ($high, $low) = (0,0); for (split /:/, $ENV{PATH} ){ next if exists $path{$_}; # remove duplicates $path{$_} = $high; ++$high; } for (@ARGV){ /^-(.*)/ and do {delete $path{$1} if exists $path{$1}; next;}; /^\+\+(.*)/ and do {--$low; $path{$1}=$low unless exists $path{$1}; next;}; /^\+(.*)/ and do {++$high; $path{$1}=$high unless exists $path{$1}; next;}; } print join ":", sort {$path{$a} <=> $path{$b} } keys %path; print "\n";