Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have a large file around 1.4 G. I am having trouble parsing it line by line and storing it in a single data structure (such as hash of arrays). My file is like this :

bos-mp96h:~ jvx$ head asgn.txt ra_uuid: a37bbde8-36ba-11e8-a697-00e081ea0e98 cms_uuid: 2d937c7e-36ba-11e8-91f1-00e081ea0e8e mpd_uuid: 6edd7a68-36b0-11e8-a120-00e081ea0e5c amLeader: 1 numAssignments = 20956857 mpg=1 mrule=140 reg=7989 score=10625 rank=0 perc=100 mp_demand=40 mpg=2 mrule=140 reg=7989 score=10625 rank=0 perc=100 mp_demand=40 mpg=3 mrule=150 reg=7989 score=0 rank=0 perc=100 mp_demand=20 mpg=4 mrule=150 reg=7989 score=10625 rank=0 perc=100 mp_demand=40
So what I am rather doing is that making different file for each index and then process that. What I want is a different file for every other "mrule". So for the above snippet, I will have two files, each for mrule 140 and mrule 150. My code for it is as follows :

#! /usr/bin/perl -w use strict; use warnings; use Data::Dumper ; my $source = shift ; my $lines_per_file = shift ; open (my $FH, "<$source") or die "Could not open source file. $!"; open (my $OUT, '>', '00000000.log') or die "Could not open destinatio +n file. $!"; my $i = 0; my $index_last = 0 ; my $index_current = 0; while(my $line = <$FH>) { next unless ($line =~ /mrule/) ; if ($line =~ /mrule=([0-9]+)/){ print $OUT $line; $i++ ; if ($1 != $index_last){ $index_current = $1 ; close($OUT); my $NEW = sprintf("%08d", $index_current); open($OUT, ">${NEW}.log") or die "Could not open desti +nation file. $! " ; } $index_last = $index_current ; } } close($FH); close($OUT);
But when I run this, I get this following error,
bos-mp96h:~ jvx$ ./partition_file.pl asgn.txt Can't use string ("00000140") as a symbol ref while "strict refs" in u +se at ./partition_file.pl line 26, <$FH> line 6.

In reply to Splitting a large file into smaller files according to indexes by cryptoperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2020-11-27 13:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?