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: Intermediate: Re: [sn0rkl3m4st3r] Sorting Chunks of a file: Edit Log



sn0rkl3m4st3r
New User

Sep 2, 2011, 5:15 AM


Views: 4045
Re: [sn0rkl3m4st3r] Sorting Chunks of a file

Thanks for all the great replies. I actually kept working on it yesterday and was able to achieve a result which is less elegant than anyone elses...by far. Here is the "final" code:




Code
   

#!/usr/bin/perl -w
#Written and tested by sn0rkl3m4st3r
#Script for sorting graph text files

#Ensure correct number of command line arguments
#and if they're incorrect, print usage and die.
if (@ARGV < 2 || @ARGV > 2)
{
die("Usage: sortit.pl <infile> <outfile>\n");
}

#Check if the file exists
if (-e $ARGV[0])
{
#open file for iteration
open(F, "<$ARGV[0]") or die("Could not open file");
open (G, ">>$ARGV[1]") or die("could not open outfile");

#initialize variables/arrays
$i = 0;
$fseven = 1;
%graph = ();
@hsharr = ();
@names = ();
$first = 1;
$inhash = 0;
$gname = "";
#read lines from file
while($line = readline(F))
{
#make sure first 7 lines are printed to the new file
if($fseven <= 7)
{
if($fseven == 7)
{
$line =~ s/\n//;
}
print G "$line";
$fseven++;
}

#check for ##*-------- line
#if in the graph section set the hash values
#and set the hash in the array
if($line =~ m/^#\*+-/)
{

#make sure we don't have null first hash/array
if($first)
{

$first = 0;

}
else
{
#this line won't be part of the array
$inhash = 0;
$i = 0;
#because this isn't the first ##*---- set we've seen
#we know we have an array to push to another array...don't ask
push(@hsharr, @graph);
#reinitialize
@graph = ();
}
}
elsif(!$first)
{
$inhash = 1;
}

if($inhash)
{
if($line =~ m/^GRAPH:/)
{
#obtain the graph name
@splitar = split(/:/, $line);
$gname = $splitar[1];
#remove leading space
$gname =~ s/^\s+//;
push (@names, $gname);
#get rid of leading spaces and enter into array
$line =~ s/^\s+//;
$graph[$i] = $line;
$i++;
}
else
{
$graph[$i] = $line;
$i++;
#print "current graph ($i): @graph\nDone\n\n";
}

}
}

#sort the list of names for comparison
@names = sort(@names);
foreach $name (@names)
{
$printed = 0;
$isequal = 0;
foreach $line (@hsharr)
{
@splitar = split(/:/, $line);
if($line =~ m/^GRAPH:/)
{
#print "trying to format...";
$gname = $splitar[1];
$gname =~ s/^\s+//;
if($gname eq $name)
{
#print "name is: $name";
print G "\n#*--------------------------------------------------------\n\n";
print G "$line";
$isequal = 1;
}
}
elsif(($line =~ m/^END_GRAPH:/) && $isequal)
{
print G "$line";
$isequal = 0;
$printed = 1;
}
elsif($isequal)
{
print G "$line";
}
}
}}
else
{
die("cannot find file");
}
close(F);
close(G);





There's a lot of stepping on toes here, but I think for the most part it does what it's supposed to do within a reasonable period of time. Also, there are a lot of place holder variables so that my iterations were kept correct. I will be, in the future, optimizing this script to be a little more efficient. Currently, much of the functionality is obviously a bit superfluous given the efficient examples you gave. Thanks a ton for your help!


(This post was edited by sn0rkl3m4st3r on Sep 2, 2011, 5:26 AM)


Edit Log:
Post edited by sn0rkl3m4st3r (New User) on Sep 2, 2011, 5:15 AM
Post edited by sn0rkl3m4st3r (New User) on Sep 2, 2011, 5:26 AM


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

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