William Martell <[EMAIL PROTECTED]> wrote: : : Hi Charles. Thank you very much for the code and the lesson. : Works perfectly!
It wasn't meant to be a finished product. Just an example to learn from. You need to, at least, add some error checking, but I am glad it works. :) : 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. No problem. Please keep asking! [ I edited the questions so they appeared under the code. ] : # 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"; : : (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? ) Yes. An unnamed hash is called an anonymous hash. I don't know if it is easier, but when I first started Randal (I think?) said something about unnecessarily using extra variables. Since, all this does is print out the column names, it doesn't make sense to define a file scoped variable for this one use. : : # Set field defaults : %ad = ( : date => '', : price => '', : bedrooms => '', : bathrooms => '', : living_areas => '', : phone => '', : address => '', : arrangement => '', : location => '', : paper => '', : ); : : (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?) Yes. Though I would call "''", blank. Maybe an empty or zero-length string, but blank sounds like it could have spaces in it. : while ( <DATA> ) { : : if ( /^Price/ ) { : : # add the last ad if it exists : print csv( \%ad ), "\n" if is_valid_ad( \%ad ); . . . : # update %ad for this line : : @ad{ qw| price bedrooms bathrooms living_areas | } : : (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?? Yes. It is called a slice. Read the section on slices in 'perldata'. : = price_fields( $_ ); : } : Another question is how does Perl know what $_ is : referring to, I guess the line that begins with Price but : I am not sure, please explain.) while ( <DATA> ) { is a short-cut for: while ( defined( $_ = <DATA> ) ) { Many programmers, especially on the list, emphasize that we should use: while ( defined( my $line = <DATA> ) ) { And then operate on $line. In my haste, I got lazy. :) Regardless, $_ (or $line) is set to the value of the current line each time the loop begins. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>