William Martell <[EMAIL PROTECTED]> wrote:
: 
: I have a question about this procedure.  I am trying
: to extract these 12 values of text from a text file.
: I am trying to get my first two hash values and this
: is the procedure that handles that.  Thanks Charles
: for all your help and mentoring.  I appreciate your
: time and consideration.

    I appreciate your saying so. Please keep asking
questions.


: When I comment out the "shift @fields;" in CODE1 and
: run it, I get the results at RESULT1.  When I comment
: out the "#push @fields; #, ( '' ) x ( 4 - @fields );"
: in CODE2, I get RESULTS2.
: 
: My question is why?  What is happening in these lines
: of code.  I dont understand why you need
: "my $line = shift;" or what this is doing?

    Look below.

: I  understand what is happening up until "shift @fields"
: to the end of the sub procedure.

    Guess I didn't help all that much then. :)

 
: What I am trying to get is RESULT3 below.  Any pointers
: on where I can find information on shift, and
: push @fields, ( " ) x ( 4 - @fields );

    Look below for an explanation. Look in 'perlfunc' for
help on 'shift' and 'push'. Or use "perldoc -f shift" and
"perldoc -f push".


: I would like to learn what this is doing and how to get
: the result I want.

    This list is probably best for getting the result you
want. But I'll need to see more code. I can't remember what
we were doing. Give us all the current code you are using
and we'll have an answer for you before long.


: [CODE1]
: sub line1fields {
:     my $line = shift; # why do I have to shift here.
:                       # I don't understand
:                       # what this is doing.

    It is short for "my $line = shift @_;". It is use
to retrieve the first element of @_. In perl any calls
to a sub fill @_ with the arguments passed to it. In
this case line1fields() is probably called as:

line1fields( 'foo' );

    When perl calls the sub it stuffs @_ with 'foo'.
The only argument in the call. The line you mention
shifts 'foo' off @_ and places its value in $line.


:  # collapse all spaces
:     $line =~ s/\s//g;
:     my @fields = split /AdNumber:|DatePosted:/, $line;
: 
:     # delete extra first field
:     #shift @fields;  # why do you have to delete
:                      # this extra first field?

    Use Data::Dumper's Dumper function to figure
this out. At the top of your script add this line.
It will import a function called Dumper into your
program. You can use Dumper to view the contents of
arrays and hashes.

use Data::Dumper 'Dumper';

    Before the shift, add a line like:

print Dumper [EMAIL PROTECTED];

:                      # Where is it coming from??

    I don't remember why I was shifting the first
field off. I assume it was something we don't want.
Use Dumper to view the @fields as explained above.

 
:     # pad missing fields
:     push @fields, ( '' ) x ( 4 - @fields );

    In this line the second @fields is evaluated
in scalar context. In that context it will return
the number of elements in @fileds.

    Here's a sample script which prints an example
for each case. Forget about how it runs for now.
Look at the output.

use strict;
use warnings;

use Data::Dumper 'Dumper';
$Data::Dumper::Terse  = 1;
$Data::Dumper::Indent = 0;

foreach ( 1 .. 5 ) {
    my @fields = ( 'foo' ) x $_;

    print "Before:\n", Dumper( [EMAIL PROTECTED] ), "\n";

    push @fields, ( '' ) x ( 4 - @fields );

    print "After:\n", Dumper( [EMAIL PROTECTED] ), "\n\n"
}

__END__


    Now go back and change 4 to some other value.
Try 10. See what it is doing?


[snipped helpful code]

: I could also really use help understanding why and
: how you use "%$ad", and "@$ad".  Any PERLDOC pointers
: here would also be appreciated.

    Those are references. Since you seem to like my writing
style take a look at this tutorial. References are explained
in the intermission section. It was written as a result of a
question on a list like this one.

http://www.perlmonks.com/index.pl?node_id=90647

    The perldoc stuff is at: perldoc perlref

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>


Reply via email to