Perl
  Home arrow Perl arrow Page 8 - Perl 101 (Part 4) - Mind Games
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 101 (Part 4) - Mind Games
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2000-06-29

    Table of Contents:
  • Perl 101 (Part 4) - Mind Games
  • Handle With Care
  • Different Strokes
  • A Little Brainwashing
  • Die! Die! Die!
  • Testing Times
  • Popguns And Pushpins
  • Shifting Things Around
  • The Real World
  • Miscellaneous Stuff

  • 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


    Perl 101 (Part 4) - Mind Games - Shifting Things Around


    (Page 8 of 10 )

    pop() and push() work on the last element of the array. If you'd prefer to add something to the beginning of the array list, you need to use unshift():
    #!/usr/bin/perl
    # open file and define a handle for it
    open(MIND,"thoughts.txt") || die("Unable to open file!\n");
    # suck the file into an array
    @file = <MIND>;
    # close file when done
    close(MIND);
    # use a loop to keep reading the file
    # until it reaches the end
    foreach $line (@file)
    {
    print $line;
    }
    # ask for input and process it
    print "How about a title?\n";
    $title = <STDIN>;
    # add title to beginning of array
    unshift (@file, $title);
    # open file for writing
    open(MIND,">thoughts.txt") || die("Unable to open file!\n");
    # print array back into file
    foreach $line (@file)
    {
    print MIND $line;
    }
    # close file when done
    close(MIND);

    And the file "thoughts.txt" will now contain a title, as entered by the user.

    Obviously, removing the first element of an array requires you to use the shift() function - we'll leave you to experiment with that one yourself.

    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Greatest Things Since Sliced() Bread} Next, two of Perl's most frequently-used functions - split() and join(). split() is used to split a string value into sub-sections, and place each of these sections into an array, while join() does exactly the opposite - it takes the various elements of an array, and joins them together into a single string, which is then assigned to a scalar variable.

    Let's take a simple example:
    #!/usr/bin/perl
    # set up a variable
    $string = "This is a string";
    # split on spaces and store in array
    @dummy = split(" ", $string);
    # print the array
    foreach $word (@dummy)
    {
    print "$word\n";
    }

    Here's the output:
    This
    is
    a
    string

    In this case, we're splitting the string using a space as the separator - each element of the split string is then stored in an array.

    You can also join array elements into a single string:
    #!/usr/bin/perl
    # set up a variable
    $string = "This is a string";
    # split on spaces and store in array
    @dummy = split(" ", $string);
    # join the words back with a different separator
    $newstring = join(":", @dummy);
    # print the result
    print "$newstring\n";

    And the output is:
    This:is:a:string

    And finally, we have the splice() function, which is used to extract contiguous sections of an array [if you don't know what contiguous means, this is a good time to find out!]. Here's an example which uses the splice() function to extract a section of an array, and assign it to a new array variable.
    #!/usr/bin/perl
    # set up a variable
    $string = "Why did the fox jump over the moon?";
    # split on spaces and store in array
    @dummy = split(" ", $string);
    # extract three words
    @newdummy = splice(@dummy, 1, 4);
    # join them together and print
    $newstring = join(" ", @newdummy);
    print "$newstring\n";

    The splice() function takes three arguments - the name of the array variable from which to extract elements, the starting index, and the number of elements to extract. In this case, the output would be:
    did the fox jump

    You should note that splice() alters the original array as well - in the example above, the original array would now only contain
    @dummy = ("Why", "over", "the", "moon?");



    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PERL ARTICLES

    - 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
    - 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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT