Hi Charles.  Thank you very much for the code and the lesson.  Works
perfectly!  I am trying to add the address portion to this but I am still
having some trouble.  I wanted to see if I could ask you questions regarding
your code for even more insight.   Thanks in advance for your time and
consideration.



#!/usr/bin/perl

use strict;
use warnings;
#use diagnostics;

use Data::Dumper 'Dumper';


(OK, right here, it looks like you just made things easier by not naming a
hash and declaring the fields then printing via csv sub procedure, and
instead just
declared an anonymous hash, is this correct? )

# column names
print csv( {
    date            => 'Date',
    price           => 'Price',
    bedrooms        => 'Bedrooms',
    bathrooms       => 'Bathrooms',
    living_areas    => 'Living_areas',
    phone           => 'Phone',
    address         => 'Address',
    location        => 'Location',
    arrangement     => 'Arrangemant',
    paper           => 'Paper',         } ), "\n";


my %ad;


while ( <DATA> ) {

    if ( /^Price/ ) {

        # add the last ad if it exists
        print csv( \%ad ), "\n" if is_valid_ad( \%ad );



(Here I am still a little foggy, but I think you are saying that in the hash
%ad there are these keys and the default values in them are blank, if they
get replaced with
real values later in the program great, and if not, they will still print as
blank.  Is this correct?)




        # Set field defaults
        %ad = (
            date            => '',
            price           => '',
            bedrooms        => '',
            bathrooms       => '',
            living_areas    => '',
            phone           => '',
            address         => '',
            arrangement     => '',
            location        => '',
            paper           => '',
        );


(I am having trouble with this,  I think you are using the array symbol
because we are dealing with a list, but that the values held in $_ and
returned by price_fields sub procedure will actually apply to the hash %ad,
Is this correct??     Another question is how does Perl now what $_ is
refering to, I guess the line that begins with Price but I am not sure,
please explain.)

        # update %ad for this line
        @ad{ qw| price bedrooms bathrooms living_areas | }
                = price_fields( $_ );
    }


    if ( /^Home/ ) {
        @ad{ qw| address arrangement | } = home_fields( $_ );
    }

    if (
                m/
                    (\S+)               # location ($1)
                    \s+-\s+
                    ([^-]+)             # paper with spaces ($2)
                    -\s+
                    (\d\d\/\d\d\/\d{4}) # date ($3)
                /x
                ) {

                # use hash slice to set fields
        @ad{ qw| location paper date | } = ( $1, $2, $3 );

        # trim trailing spaces in paper field
        $ad{paper} =~ s/\s+$//;
    }
}

print csv( \%ad ), "\n" if is_valid_ad( \%ad );

sub home_fields {
        # This is a dummy function
    return ( '', '' );
}

sub price_fields {

(what is happening when you write 'my $line = shift' ???)

    my $line = shift;

        # collapse all spaces
    $line =~ s/\s//g;
    my @fields = split /Price:|BD:|BA:|LA:/, $line;


(why is there an extra first field??)

    # delete extra first field
    shift @fields;


(I totally don't understand what you are doing here, but I'll look it up,
you are welcome to comment if you like.)
    # pad missing fields
    push @fields, ( '' ) x ( 4 - @fields );

    return @fields;
}

sub is_valid_ad {
    my $ad = shift;



(I really don't understand what %$ad is,  I think it is a reference to a
hash, but I am not sure., I'll look it up, you are welcome to comment if you
like.)
    foreach my $value ( values %$ad ) {

        # return true if one field is valid
        return 1 unless $value eq '';
    }

    # return false if we get here
    return;
}

sub csv {
    my $ad = shift;

    # if you need to change field order
    # do it here
    my @field_order = qw|   price bedrooms bathrooms phone
                            address arrangement
                            location paper date |;

    # add double quotes (")
    foreach my $value ( values %$ad ) {
        $value = qq("$value");
    }

(I don't understand this @$ad , I think this is a reference to an array, but
not sure.  I will try to look this up as well)
    # use a hash slice to order the fields
    return join ',', @$ad{ @field_order };
}

__END__
----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $725     BD: 3     BA: 1     LA:
Home 3bd-1ba-1,ch/a new carpet & paint fence stove $725+dep. 972-289-2098

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $950     BD: 3     BA: 1.5     LA:
Home 3bd/1.5ba/2 1620 Summitt $950+dep Sec 8ok 469-525-0204; 214-957-8176

Mesquite  -  The Dallas Morning News  -  04/08/2004



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to