spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Perl OO - Creating a list of objects


You have two main problems

  • Your initialisation of $self

    $self = {
        _id => $id,
        _title => (),
        _words => ()
    };
    

    is very wrong, because the empty parentheses () add nothing to the structure. If I dump $self after this I get

    { _id => 1, _title => "_words" }
    

    You are also blessing $self twice, but there is no problem with that: it is more of an indication that you don't understand what you're doing.

  • There is no need to initialise a hash element for the first occurrence of a word: Perl will do that for you. In addition you should be initialising the count to 1 and not to 0.

Here's an example of your code working as it should. I have used Data::Dump to display the contents of the three document objects.

use strict;
use warnings;

package Document;

sub new {
    my ($class, $id) = @_;

    my $self = {
        _id => $id,
        _words => {},
    };

    bless $self, $class;
}

sub pushWord {
    my ($self, $word) = @_;

    ++$self->{_words}{$word};
}



package main;

use Data::Dump;

my $doc1 = Document->new(1);
my $doc2 = Document->new(2);
my $doc3 = Document->new(3);

$doc1->pushWord($_) for qw/ a b c /;
$doc2->pushWord($_) for qw/ d e f /;
$doc3->pushWord($_) for qw/ g h i /;

use Data::Dump;

dd $doc1;
dd $doc2;
dd $doc3;

output

bless({ _id => 1, _words => { a
=> 1, b => 1, c => 1 } }, "Document")
bless({ _id => 2, _words => { d => 1, e
=> 1, f => 1 } }, "Document")
bless({ _id => 3, _words => { g => 2, h
=> 2, i => 2 } }, "Document")

Categories : Perl

Related to : Perl OO - Creating a list of objects
Creating an Array with class objects and with derived Objects C++
First, you need to fix a syntax error: unlike Java or C# where array type is indicated by a pair of empty square brackets after the type name, C++ "understands" square brackets after the name of the variable change the type of the variable to an array type. Die[] regDieBag[10]; // ^^ This syntax is incorrect Now, when you see these lines together Die regDieBag[10]; // ^ No asterisk and reg

Categories : C++
Creating Objects for Game in C++
1. Pong Pong; is illegal: you can't declare an object with a type name as identifier. This could've been Pong pong. 2. Pong::enable2D() is illegal: enable2D isn't a static function, but a member (per-instance) function and thus you've to call it via an object, like pong.enable2D();. 3. extern Pong pong; // in the header Pong pong; // and both of these in the source Pong Pong; T

Categories : C++
Objects creating an instance of each other?
If they reference each other I think the design should be changed. You could: 1. move A and B into one combined class 2. refactor out the code used by both into new class C. Then in both A and B you could then reference C I'd be more inclined to do 2 as personally I'm not a fan of big classes all high you give no information on what A and B contain and how big they are. For a more full answe

Categories : Java
Creating several objects on form submission
If the behaviour you want is "Every time i deliver a Share, I also want to create a ShareLink", then you're right. The controller is not the best place to do this. Probably would be better to override the deliver method in Share so it can insert a new ShareLink def deliver super ShareLink.create(params:{...}) end But this supposed you have enough information in your Share object to crea

Categories : Ruby On Rails
Creating an array of objects with txt file values
You certainly want to read up about the Collection classes in java.util. There are classes that match or contain the words that you mentioned are looking for: ArrayList (for array list) and Hashtable (for table). Although, in case of Hashtable you probably want to use the newer HashMap instead.

Categories : Java
Recently Add
unable to connect selenium webDriver through perl bindings
SSL error calling Perl web service via https using LWP
SOAP::Lite always faults in debug mode
perl referencing a hash outside of the scope that populated it (histogram)
Perl script to print out all the lines containing a keyword and the line below it
PERL Unicode Escape string pass from command line argument wrong output
Perl process crashes after handling signal
Marpa: Can I explicitly disallow keywords as identifiers?
Perl: Import text, containing ÅÄÖ, from file
error using perl - Proc::ProcessTable. works in cmd line, not in .pl file
DBIx::Class chaining resultsets with same table in prefetch
Time as input, print timetable in Perl
compare values of different tables perl
Automatically increasing ID generation using PERL
Syntax Error, first line after dostuff
Delete files in a folder using Perl
Perl '-d' operator is not detecting a directory
Perl appending text to new line
write_timeout in IO::Socket::Timeout
Using ForkManager and Perl properly?
Is a cyclic dependency solved with require?
Posting metric to google analytics from perl script
How can I check if a user enters my page the first time?
Sorting arrays of intervals in perl?
Perl: How to get the link target of a soundcloud feed mp3
Uninitialized value in concatenation
Perl code - pipe "|" in `open()` statement
perl command line backticks with matrix
Perl OO - Creating a list of objects
extract gene-id + function annotation from .gff
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.