Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

by CharlesClarkson (Curate)
on Nov 06, 2001 at 10:23 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-05-26 04:45 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.