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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 7
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT