Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: How do I create a sort sub on-the-fly?

by CharlesClarkson (Curate)
on Nov 06, 2001 at 10:23 UTC ( [id://123522]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I create a sort sub on-the-fly?
in thread How do I create a sort sub on-the-fly?

I originally used Sort::Fields to create the report, but as I mention below most beginners don't like to use modules outside the standard ones.

#!/usr/bin/perl use strict; use diagnostics; use Sort::Fields; my (@records, @report); { my $data_file = 'test.dat'; open my $fh, $data_file or die "Can't open $data_file: $!"; while (<$fh>) { # skip blank and commented lines next if /^\s*#/; next if /^\s*$/; # We'll look for lines that describe a report: if (/report:\s+sort\s+(\S*)/ ) { @report = split /,/, $1; next; } push @records, $_; } } my (@columns, @sort_description); { my %field = ( source => 1, time => 2, sip => 3, sport => 4, dip => 5, dport => 6, hits => 7, acl => 8, lnum => 9 ); foreach (@report) { my ($name, $order) = split /-/; my $suffix = $name =~ /source|time/ ? '' : 'n'; if ($order eq 'd') { push @sort_description, "$name:\t\tdescending\n"; push @columns, "-$field{$name}$suffix"; } else { push @sort_description, "$name:\t\tascending\n"; push @columns, "$field{$name}$suffix"; } } } { ### store report data in file my $report_file_name = "out.txt"; open my $fh, '>', $report_file_name or die "Can't Open $report_file_name: $!"; my $date = localtime; $date =~ s/ /-/g; print $fh qq|\t\tREQUEST FOR SORTING\n\n|, @sort_description, qq|\n\nFILE WAS GENERATED ON: $date\n\n|, fieldsort ',', \@columns, @records; } __END__



Thanks for your reply,
Charles K. Clarkson

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found