Perl Programming Page 4 - Perl Lists: The Split() Function |
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...
blog comments powered by Disqus |
|
|
|
|
|
|
|