![]() |
Splitting Record into Fields![]() ![]() ![]() ![]() ![]()
This example will show you how to read a file line-by-line and break the input records into fields based on a separator string. The file, FIELDS.DAT, will be used with the following contents: 121 2-.Jan:Jaspree: Painter 3453: Kelly: Horton:Jockey The individual fields or values are separated from each other by the colon (:) character. The split() function will be used to create an array of fields. Then a foreach loop will print the fields. The coding below shows how to input lines from a file and split them into fields.
@fieldList = qw(fName IName job age);
The first line of this program uses the qw() notation to create an array of words. It is identical to @fieldList = ("fName", "IName", "job", "age"),- but without the distracting quotes and commas. The split statement might require a little explanation. It is duplicated here so that you can focus on it.
@data{@f ield List) = split(/:/, $-, scalar @fieldList); Let's use the first line of the input file as an example. The first line looks Ilk this:
121 2:Jan:Jaspree: Painter
The first thing that happens is that split creates an array using the colon as the separator, creating an array that looks like this:
("1212", "Jan", "Jaspree", "Painter") You can substitute this list in place of the split() function in the statement
@data{@fieldList} = ("1212", "Jan", "Jaspree", "Painter"); And, you already know that @fieldList is a list of field name. So, the statement can be further simplified to:
@data(" f Name", "IName", "job", "age")=
This assignment statement shows that each array element on the right is with a key value on the left so that four separate hash assignments are place in this statement.
|
|
PERL Perl Program Data Types Types of Variables Operator Types Functions String Functions Array Functions Expressions Statement Blocks The if Statement Unless Statement While Loops Until Loop For Loop Foreach Loop Jump Keywords Subroutines Perl References Die Function Exit Function Localtime Function Standard Files File Test Operators File Functions Opening Files Reading into Hash Printing Revisited Globbing Splitting a Record into Fields Perl's Special Variables Regular Expression Simple True-False Searches Inexact Matches in Regular Expressions Matching Any Character Characters with Class Special Locations Quantifiers Greedy Matching Modifiers Localtime Function Subsituations Letter for Letter Translations Generating A Simple Report Defining a Report Template Specifying the Picture Line Specifying the Argument Line Adding a Header Writing the ReportDomain Names Home Importance of a domain name How does a web domain name work? Domain name syntax Choosing a domain name Domain Transfer Domain Parking IP AddressWeb Design HTML DHTML XML JAVASCRIPT VBSCRIPT PHOTOSHOPWeb Hosting Web Server Web Server Software Microsoft Internet Information Server Web hosting ISP Shared web hosting Virtual web hosting Dedicated web hosting Web hosting colocation Managed web hostingE Commerce Principles of E-Commerce E-Banking Electronic Payment System E-Security Credit Cards Smart Cards
The Mailbox POP Server SMTP Server IMAP Protocol |
| Home | Web Hosting | Web Design | Sitemap |
| Copyright (C) 2007. Web Domain design hosting. All rights reserved. |