Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I am new to modules and was wondering if there is a module or a way :D to handle this kind of of problem. If I am trying to insert something in a file in a certain position based on the title , on other wards , let's say I want to insert something in a file which has the following :
status Monday Tuesday Wednesday good party
what if I want to open that file and append to it beer under Monday so it will look like this :
status Monday Tuesday Wednesday good party vgood beer
Hints on a modules or best way on doing that , I would appreciae it it. thanks

Replies are listed 'Best First'.
Re: inserting in different places in a file
by NetWallah (Canon) on Feb 27, 2004 at 22:07 UTC
    This smells like the same homework as This node and This one. Please look for responses in those.

    "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."
      what if it smells like homework , I am asking for ideas only :) , I don't need a solution . thanks anyway
        OK - here is some code I wrote (part of it shamelessly plagerized) because I could not find a module to satisfactorily parse your kind of data. This sould probably be retro-fitted into Text::FixedLength.

        If you can figure out how to apply the 2 subroutines to your homework, power to you!:

        use strict; #12345678901234567890123 my $str= "This That Other Next Last "; my $str2="One Fine Morning in May"; my $str3="Some format happened"; #my $fmt= [qw(6 6 7 4)]; # Lengths of each piece my $fmt = getLengths($str); my ($count,$blanklen,@lengths); #print " Length = $_\n" for @{getLengths($str)}; for ($str, $str2, $str3){ print "----Str=[$_] Fmt=@$fmt\n"; print " [$_]\n" for getPiecesByFixedLength($_, $fmt); print "--Last--\n"; } # -------------------------------------------------------------------- +-------- # Subroutine: getPiecesByFixedLength - given a string, delimiter, and +format return an arrray # -------------------------------------------------------------------- +-------- sub getPiecesByFixedLength { my $OriginalStr = shift || die 'getPiecesByFixedLength: need a strin +g'; my $format = shift || die 'getPiecesByFixedLength: need a forma +t'; ref $format eq 'ARRAY' or die "Second param Must be array ref\n"; my @out = (); foreach ( @$format ) { s/\D//g; # - save only digits my $ThisPiece = substr($OriginalStr,0,$_); $ThisPiece =~ s/^\s+//; $ThisPiece =~ s/\s+$//; push @out, $ThisPiece; substr($OriginalStr,0,$_) = ''; } return @out; } # -------------------------------------------------------------------- +-------- sub getLengths{ # Return a ref to an array containing Lengths of pieces of the strin +g.. my $OriginalStr=shift || die "getLengths needs string param\n"; my ($count, @ret); foreach(split /(\s+)/,$OriginalStr){ m/^\s+$/ and $ret[$count - 1]+= length($_) , next; $ret[$count] = length($_); #print "Got Piece $count [$_] Len " . length($_) . "\n"; $count++; } wantarray? return @ret : return \@ret; } # -------------------------------------------------------------------- +--------

        "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."
Re: inserting in different places in a file
by TomDLux (Vicar) on Feb 28, 2004 at 20:04 UTC

    Personally, I would take a different approach, which might be suitable for your needs or might be more general and more complicated than you want to get into. While the file is inherently row-oriented, I see your problem as essentially column-oriented. As a consequence, I would store each day's data as an array. In this case, any row only has data for one day. Is that guaranteed? or simply a characteristic of the sample data? For that matter, can there be more than one entry for a single day? If only one, a simple hash mapping 'day of the week' to 'entry' is sufficient .... though handling 'status is slightly more complicated.

    In general, understanding the requirements to a greater extent would lead to a more general, more robust, simpler solution. Otherwise, we're trying to solve the wrong problem, and any 'solutions' will be lucky to be useful.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA