Derp
Derp

Reputation: 13

I am having difficulty printing certain information from a __DATA__ source

I am having difficulty printing just the zip code for Massachusetts for a home work assignment. The information is pulled from DATA and this can not be edited to make it easier.

I'm not asking for a direct answer just maybe some guidance of different methods this is my first programming language so I am still trying to understand. Thank you for your time.

I have been able to print the entire address successfully using the first section of code below.

I tried to split $address again by using the second section of code, but keep getting a fault for too many arguments for split at line 5. I've tried different formats of this code non of which are successful.

use warnings;
my ($name, $phone, $address, $Bday, $salary);
while (<DATA>){
($name, $phone, $address, $Bday, $salary) = split(":", $_);

print "$address\n" if $address =~ m/MA/g;

}

use warnings;
my ($name, $phone, $address, $Bday, $salary);
while (<DATA>){
($name, $phone, $address, $Bday, $salary) = split(":", $_);
($address) = split (",",$home,$town,$state,$zip);

print "$zip\n" if $address =~ m/MA/g;

}

__DATA__
Tommy Savage: 408-724-0140:122 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Lesle Kerstin: 408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach: 408-253-3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy: 293-259-5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop: 245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf: 846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder: 397-857-2753:74 Pine Street, Deadborn, MI 23874:3/28/45:245700 
James Ikeda: 834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz: 327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz: 385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

Output needs to just be the zip codes for MA

02133
34756

Upvotes: 1

Views: 47

Answers (1)

GMB
GMB

Reputation: 222622

The error message that you are getting indicates that you are not using split correctly. I guess that you actually meant:

($home,$town,$state,$zip) = split(",", $address);

instead of:

($address) = split (",",$home,$town,$state,$zip);

But fixing this will still not return the result you expect, mainly because the state and the zip code are not separated by a comma, but by a space. As your code is, $zip will end up undefined, while $state will contain both the state name and zip code.

Other considerations:

  • always use strict and use warnings
  • split usually takes a regular expression as first argument instead of a plain string ; also it takes $_ as default argument, as many other perl built-ins.

Consider:

use warnings;
use strict;

while (<DATA>){
    my $address = (split /:/)[2];
    if ($address =~ m/MA/g) {
        my ($zip) = ($address =~ /(\d+)$/);
        print "$zip\n";
    }
}

Explanation:

  • since the adress is the only part that you need, don't capture the other parts; this avoids instantiating useless variables

  • the zip code is made of a series of digits at the end of the adress


Here is a shorter syntax that just uses a single regular expression to parse the string, capture the relevant part and print it:

while (<DATA>){
    print "$1\n" if /^[^:]+:[^:]+:[^:]+MA (\d+):/;
}

Regexp explanation:

  • ^ is the beginning of the string
  • [^:]+: represents a group of as many characters as possible other than : (at least one), followed by :
  • the third group of non-: characters should end with string MA, a space, and a series of digits; the parens around the digits indicates the this part of the string should be captured (it will then be available as $1)

Upvotes: 2

Related Questions