Perl Lists: The Split() Function - Assigning a List to Another List (Page 4 of 4 )
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...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |