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



Zhris
User

Sep 1, 2011, 3:01 PM


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

Hey,

Theres a number of ways the data could be constructed -> sorted -> printed. Heres my rough method which merely uses nested array references:


Code
#! /usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

my $b_regex = q{^#\*\-+}; #break
my $gn_regex = q{^GRAPH: <(.*?)>}; #graph name

my $graphs = [];
my $graph_name = q{};
my $chunk = [];

while (my $line = <DATA>) {
chomp $line;

push @$chunk, $line;

if ($line =~ m/$b_regex/) {
push @$graphs, [$graph_name, $chunk];
($graph_name, $chunk) = (q{}, []);
}
elsif ($line =~ m/$gn_regex/) {
$graph_name = $1;
}
}

#print Dumper($graphs);

foreach my $graph (sort { $a->[0] cmp $b->[0] } @$graphs) {
{
local $" = "\n";
print "@{$graph->[1]}\n";
}
}

__DATA__
#*----------------

GRAPH: <graphname2>

stuff

#*----------------

GRAPH: <graphname1>

stuff

#*----------------

GRAPH: <graphname3>

stuff

#*----------------


Chris


(This post was edited by Zhris on Sep 1, 2011, 4:06 PM)


Edit Log:
Post edited by Zhris (User) on Sep 1, 2011, 3:02 PM
Post edited by Zhris (User) on Sep 1, 2011, 3:14 PM
Post edited by Zhris (User) on Sep 1, 2011, 3:24 PM
Post edited by Zhris (User) on Sep 1, 2011, 4:06 PM


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

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