Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

inserting into file

by grashoper (Monk)
on Mar 24, 2009 at 15:18 UTC ( #752897=perlquestion: print w/replies, xml ) Need Help??

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

I am not sure why this doesn't work, I am wanting to insert the label Date and a \n on each line that a date appears in my test files, so that I can populate a hash to insert into a db.
use strict; use warnings; my $text; while(<DATA>) { chomp($_); if($_ =~ /\d{1,2}\/\d{1,2}\/\d{4}/) { unshift($text .= "Date\n" unless $. == 1); $text .= $_; } else { $text .= "\n,".$_; } } print $text; __DATA__ 1/3/2007 12:20:01 AM Login,12.588309 SearchLoad,9.432586 SearchCount,20:0.196329 SearchResults,7.418672 SearchSave,3.616305 SearchDelete,2.066482 SearchDetails,6.873061 ClientAdd,0.784989 CMALoad,1.859894 CMASave,3.249620 CMADelete,0.450952 ClientDelete,0.305768 Logout,0.823402 1/3/2007 12:49:22 AM Login,10.958312 SearchLoad,13.644527 SearchCount,41:0.483233 SearchResults,7.027840 SearchSave,4.222601 SearchDelete,0.305821 SearchDetails,7.443877 ClientAdd,1.552915 CMALoad,1.202711 CMASave,5.285398 CMADelete,0.233119 ClientDelete,0.425521 Logout,0.560862

Replies are listed 'Best First'.
Re: inserting into file
by kennethk (Abbot) on Mar 24, 2009 at 15:30 UTC

    The presence of an unshift on line 9 is a syntax error as unshift requires a destination array which is not present in your example - I can only assume this is an artifact from a previous attempt involving a file slurp.

    Why do you not want to insert "Date" on the first line of the file (unless clause of line 9, $.)? Particularly considering that it is a date, I would think that would be a desirable result.

    Based on this code and the contents of insert label into file, perhaps you want something like this?

    use strict; use warnings; my $text; while(<DATA>) { chomp($_); if($_ =~ /\d{1,2}\/\d{1,2}\/\d{4}/) { $text .= "\nDate," unless $. == 1; $text .= $_; } else { $text .= ",\n".$_; } } print $text; __DATA__ 1/3/2007 12:20:01 AM Login,12.588309 SearchLoad,9.432586 SearchCount,20:0.196329 SearchResults,7.418672 SearchSave,3.616305 SearchDelete,2.066482 SearchDetails,6.873061 ClientAdd,0.784989 CMALoad,1.859894 CMASave,3.249620 CMADelete,0.450952 ClientDelete,0.305768 Logout,0.823402 1/3/2007 12:49:22 AM Login,10.958312 SearchLoad,13.644527 SearchCount,41:0.483233 SearchResults,7.027840 SearchSave,4.222601 SearchDelete,0.305821 SearchDetails,7.443877 ClientAdd,1.552915 CMALoad,1.202711 CMASave,5.285398 CMADelete,0.233119 ClientDelete,0.425521 Logout,0.560862

    with output

    1/3/2007 12:20:01 AM, Login,12.588309, SearchLoad,9.432586, SearchCount,20:0.196329, SearchResults,7.418672, SearchSave,3.616305, SearchDelete,2.066482, SearchDetails,6.873061, ClientAdd,0.784989, CMALoad,1.859894, CMASave,3.249620, CMADelete,0.450952, ClientDelete,0.305768, Logout,0.823402 Date,1/3/2007 12:49:22 AM, Login,10.958312, SearchLoad,13.644527, SearchCount,41:0.483233, SearchResults,7.027840, SearchSave,4.222601, SearchDelete,0.305821, SearchDetails,7.443877, ClientAdd,1.552915, CMALoad,1.202711, CMASave,5.285398, CMADelete,0.233119, ClientDelete,0.425521, Logout,0.560862

      That is what I wanted thanks,I prime the while with a print \ndate now I just have to figure out how to get my hash values into my db and I should be in good shape, one thing thats really throwing me for a loop is that the date is a string, and I am having trouble figuring out how to make it a sql small date time. someone sent me a link to sql standards the other day so much stuff not sure where to look first. I agree which is why I was trying to figure out how to add it to a %hash previously as a key, this is a one time issue, I will be recording values differently in the future so that hash is well formed so should not be an issue again. however my data.txt file did not contain a date label so my hash was not initialized properly I wanted columname->value, and date is columname now I need to fix my input files, then I should be able to use variables to loop through and get my records into the db.
      use Win32::ODBC; use DBI; $DSN="Ptest"; $db = new Win32::ODBC("DSN=Ptest;UID=ptalerter;PWD=Ha55le12;"); if(!($db)){ print "Error connecting"; exit; } else { print "Hey you are connected \n"; } $SqlStatement = "insert into AllResults (Date,Login,Searchload,Search +Count,SearchResults,SearchSave,SearchDelete,SearchDetails,TaxLoad,Tax +Results,TaxDetails,ClientAdd,CMALoad,CMASave,CMADelete,ClientDelete,L +ogout) Values ('getdate()', '1','2','3','4','5','6','7','8','9','10', +'11','12','13','14','15','16')"; if ($db->Sql($SqlStatement)){ print "SQL failed.\n"; print "Error: " . $db->Error() . "\n"; $db->Close(); exit; } while($db->FetchRow()){ undef %Data; %Data = $db->DataHash(); print $db; } db->Close(); close file;
        ... I prime the while with a print \ndate ...
        Given the example data, there seems to be no reason to treat the first record differently from the succeeding ones. You should realize that to do so is to point a loaded gun at the foot of the maintainer of this code – which may be you!
Re: inserting into file
by olus (Curate) on Mar 24, 2009 at 15:30 UTC

    What kind of arguments are you giving to unshift? If you just want to add a piece of text before a line of text just $_ = "Date\n".$_ unless $. == 1;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-05-16 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (63 votes). Check out past polls.

    Notices?