SunQuest
 
       Perl
  Home arrow Perl arrow Page 3 - Perl: Appending and Writing to Files
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl: Appending and Writing to Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-11-14

    Table of Contents:
  • Perl: Appending and Writing to Files
  • Writing to Files
  • Overwriting Without Deleting Everything
  • How to Delete a File
  • More Ways to Test Files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl: Appending and Writing to Files - Overwriting Without Deleting Everything


    (Page 3 of 5 )

    Okay, so maybe we were a little hasty in overwriting the entire table. To be fair, there is not a way to delete a single line of data with the Overwrite command. But we can trick the program using our gigantic brains. For this example we are going to work with our original four sets of data. We are going to delete Daredevil (our second record it seems) from the table. Observe!


    $my_file="superhero.txt";


    open(PLOT, $my_file) || die('Oops the file is broked!');

    @my_data=<PLOT> ; #stores the data from the file in an array called @my_data

    close(PLOT);


    splice(@my_data,1,1);


    open(PLOT,">$my_file") || die("File won't open");

    print PLOT @my_data;

    close(PLOT);

    The splice command works by look at the command and removing the array element you indicate (1,1...remember arrays start at the number 0. Since daredevil is the second element, we use 1).

    When you are finished, this is the data that will remain in the file:

      The Incredible Hulk|Super Strength|I rip my pants


      Apache Chief|the ability to grow Very Tall|I Wear a skirt


      Aqua Man|the ability to communicate with fish|deep fryers and tartar sauce

    More Perl Articles
    More By James Payne


       · > we must first escape using the backslash(/).Do you mean '\' ? -pamag
       · Hey thanks for catching that pamag; I did indeed intend for that to read:...
     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway