Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Parsing a Large file with no reason

by Cristoforo (Curate)
on Jan 29, 2010 at 02:25 UTC ( [id://820285]=note: print w/replies, xml ) Need Help??


in reply to Re: Parsing a Large file with no reason
in thread Parsing a Large file with no reason

It is so slow on large files because for each matching record, you loop through all 80,000 lines; So, if you had had 4000 matching records, you would have 4000 * 80000 = 320,000,000 iterations. There must be a better method I think.

And I don't know if you can 'tie' the same file (as an array) while opening it for reading (both at the same time).

Note that I set the input record separator, $/, to ---- lsattr, (with a space following lsattr), to read a record at a time.

Not seeing more sample data, I made a guess at what might work and it did work with your sample data. But again, it's difficult to tell.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $base = $ARGV[0] or die "Must supply a filename to open. $!"; open my $fh, "<", $base or die "Unable to locate file: $!\n"; my %data; { local $/ = "---- lsattr "; while (<$fh>) { chomp; next unless /^-El\s+(\S+)/; my $vg = $1; next unless /^label\s+(\S+)/m; my $label = $1; next if $label eq "None"; next unless /^lvserial_id\s+(\S+)/m; my $name = $1; next unless /^size\s+(\d+)/m; my $size = $1; @{ $data{ $vg } }{ qw/ label name size / } = ($label, $name, $ +size); #print "VG: $vg : MOUNT: $label : LV_name: $name : SIZE: $size +\n"; } } print Dumper \%data;
Update: The data structure created above will only work if there is only 1 record for each sought key, ($vg). If there is more than 1 record with the same key, the data structure will only contain the last fields parsed from the file. It will silently give you incorrect results.

That said, I would need to know more about your file to be able to suggest a suitable data structure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found