Home arrow Perl Programming arrow Page 4 - Perl Lists: The Split() Function

Assigning a List to Another List - Perl

In this fourth part of our series on Lists, we will start off with the split() function and hopefully end by covering hashes. In our last article, we covered the splice() function, which we used to add, remove, and replace elements in a list. We then used it to create variables and arrays. We also worked with some operators to repeat a list and to create sequential lists.

TABLE OF CONTENTS:
  1. Perl Lists: The Split() Function
  2. Using Split() On a String
  3. Limiting the Amount of Splits
  4. Assigning a List to Another List
By: James Payne
Rating: starstarstarstarstar / 6
April 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

This is a pretty simple thing to do. Say you have a list of your grades for the semester. As you take your tests and whatnot, you want to create a new list showing your grades up to a certain point. Here is how you would do so:


#!/usr/bin/perl

@FirstMonth=('A ','B ','A ','D ');

@SecondMonth=('A ','B ','C ','A ',@FirstMonth);

print @SecondMonth;

This adds the values in @FirstMonth to the end of the @SecondMonth list, resulting in:

  A B C A A B A D

We could continue this process as the months go on:


#!/usr/bin/perl

@FirstMonth=('A ','B ','A ','D ');

@SecondMonth=('A ','B ','C ','A ',@FirstMonth);

@ThirdMonth=('F ','F ','A ','A ',@SecondMonth);

@FourthMonth=('A ','A ','A ','A ',@ThirdMonth);

print @FourthMonth;

You will note that I did not write @FourthMonth=('A','A','A','A',@FirstMonth,@SecondMonth,@ThirdMonth). This is because it would have been redundant and added even more fields. Remember that the @SecondMonth already contains all of @FirstMonth's value. And likewise, @ThirdMonth contains all of @FirstMonth and @SecondMonth's values. And so forth.

The result of this code is:

  A A A A F F A A A B C A A B A D

And while we are at it, we can also assign variables to the mix as well. Here it is in code:


#!/usr/bin/perl

@FirstMonth=('A ','B ','A ','D ');

@SecondMonth=('A ','B ','C ','A ',@FirstMonth);

@ThirdMonth=('F ','F ','A ','A ',@SecondMonth);

@FourthMonth=('A ','A ','A ','A ',@ThirdMonth);

$ExtraCredit="A";

@Total=(@FourthMonth, $ExtraCredit);

print @Total;

The end result:

  A A A A F F A A A B C A A B A D A

Looks like you had some extra-curricular activity in their a few times with those F's. Wink, wink.

Well, that's all the time we have for this one. We still have a ways to go on Lists and Hashes, including Multi-Dimensional lists and the List::Util, but we're making progress. Be sure to join me next time as we continue, and hopefully one day, finish this discussion.

Till then...



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: