Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl newbie confusion

Status
Not open for further replies.

CodeButcher

Programmer
Oct 8, 2007
1
US
Longtime programmer, first time perl'er.

I have the following code that i'm trying to convert from Perl to C#.

Code:
sub make_vector {
	my ( $self, $doc ) = @_;
	my %words = $self->get_words( $doc );	
	my $vector = zeroes $self->{'word_count'};
	
	foreach my $w ( keys %words ) {
		my $value = $words{$w};
		my $offset = $self->{'word_index'}->{$w};
		index( $vector, $offset ) .= $value;
	}
	return $vector;
}

I don't understand the following lines:
1.) my $vector = zeroes $self->{'word_count'};
my guess is that it initializes a vector to have the same number of items as word_count array with values set to 0. Is that right?

2.) my $offset = $self->{'word_index'}->{$w};
My guess is that it's pulling the value of the hash word_index at position or key value of $w. Is that right?
 
zeroes isn't a perl function. Have a look at the code for a sub called zeroes, and see what it does (or post it here for an analysis).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
2.) my $offset = $self->{'word_index'}->{$w};
My guess is that it's pulling the value of the hash word_index at position or key value of $w. Is that right?

No not how I read it anyhow - but i'm sometimes wrong!

You can have a hash of hashes, nested multiple times deep, take this XML parser for example...
Code:
# NOTE $ret is the returned SOAP XML from an LWP request.
    # Parse XML response
    use XML::Simple;
    my $ref = XMLin($ret);

$ref now equals a hash reference, and that hash contains other hashes, each tier represents a node in the XML SOAP response.

So I can now get the value of a node like so...
Code:
if($ref->{'soap:Body'}->{'soap:Fault'}->{'faultstring'} eq "My Value"){
        &do_something;
        exit();
    }

Hope that makes sense :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
And then I noticed that $self is an object! d'oh.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top