Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to get 0 to initialize a value

by AnomalousMonk (Bishop)
on Apr 30, 2019 at 04:31 UTC ( #1233173=note: print w/replies, xml ) Need Help??


in reply to How to get 0 to initialize a value

Others have pointed out that the OPed code seems to work just fine once the
    my $first_novel = $novel_list->[$first_book];
statement is fixed to be
    my $first_novel = $novel_list[$first_book];

Somewhat off-topic, may I suggest that a better approach might be to use a hash:

use strict; use warnings; my @novel_list = <DATA>; chomp @novel_list; my %characters = ( Bink => 'M1, M2, m3, m4, 6, m7, M9, m11, m13, 14, 17, m19, m21, M2 +2, m23, m31, 35, 36, m37, m40, m41', Quan => '0', ); for my $character (sort keys %characters) { my @appearances = split m{ \s* , \s* }xms, $characters{$character}; my $first_book = $appearances[0]; my $first_type = $first_book =~ /^M/ ? 'major' : $first_book =~ /^m/ + ? 'mentioned' : undef; (my $i_first_novel = $first_book) =~ s/\D+//g; my $first_novel = $i_first_novel ? qq{"$novel_list[$i_first_novel]"} : "[$novel_list[$i_first_novel]]" ; print "$first_novel is the first novel '$character' was in. \n"; } __DATA__ Other source A Spell for Chameleon The Source of Magic Castle Roogna Centaur Aisle Ogre, Ogre Night Mare Dragon on a Pedestal Crewel Lye: A Caustic Yarn Golem in the Gears Vale of the Vole Heaven Cent Man from Mundania Isle of View Question Quest The Color of Her Demons Don't Dream Harpy Thyme Geis of the Gargoyle Roc and a Hard Place Yon Ill Wind Faun and Games Zombie Lover Xone of Contention The Dastard Swell Foop Up in a Heaval Cube Route Currant Events Pet Peeve Stork Naked Air Apparent Two to the Fifth Jumper Cable Knot Gneiss Well-Tempered Clavicle Luck of the Draw Esrever Doom Board Stiff Five Portraits Isis Orb Ghost Writer in the Sky
Output:
c:\@Work\Perl\monks\Lady_Aleena>perl novel_character_list_2.pl "A Spell for Chameleon" is the first novel 'Bink' was in. [Other source] is the first novel 'Quan' was in.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: How to get 0 to initialize a value
by Lady_Aleena (Curate) on Apr 30, 2019 at 10:29 UTC

    I found the problem in my data. I made the assumption my data was correct. I should know better. Sorry for taking up your time.

    I took those values OUT of the hash they were in. I wasn't about to post a hash that has 2,198 keys and each of those keys has at least 8 keys. I simplified my code for this problem. And yes, I know I made a mistake with the @novel_list, but the warning still remains. The actual code is...

    for my $key (keys %$characters) { my $character = $characters->{$key}; my $name = $character->{Name}; # about 30 more lines of code to munge the data my @novels = split(/, /, $character->{book}); # line 132 in code my $first_book = $novels[0]; my $first_type = $first_book =~ /^M/ ? 'major' : $first_book =~ /^m/ + ? 'mentioned' : undef; # line 134 in code $first_book =~ s/\D//g; # line 135 in code $character->{intro}->{book} = $book_list[$first_book]; # line 136 in + code $character->{intro}->{type} = $first_type; $character->{book} = \@novels; # about 30 more lines of code to munge the data }

    The hash would be (I think)...

    my $characters = { Bink => { Name => 'Bink', book => 'M1, M2, m3, m4, 6, m7, M9, m11, m13, 14, 17, m19, m21, M2 +2, m23, m31, 35, 36, m37, m40, m41', }, Quan => { Name => 'Quan', book => '0', }, };

    I just didn't feel like making the hash for it. $character->{book} is still coming back uninitialized for Quan.

    The errors are...

    character_tests.pl: Use of uninitialized value in split at character_t +ests.pl line 132 character_tests.pl: Use of uninitialized value $first_book in pattern +match (m//) at character_tests.pl line 134 character_tests.pl: Use of uninitialized value $first_book in pattern +match (m//) at character_tests.pl line 134 character_tests.pl: Use of uninitialized value $first_book in substitu +tion (s///) at character_tests.pl line 135 character_tests.pl: Use of uninitialized value $first_book in array el +ement at character_tests.pl line 136
    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena

      SSCCE:

      use strict; use warnings; use Test::More tests => 1; use Test::NoWarnings; my $characters = { Bink => { Name => 'Bink', book => 'M1, M2, m3, m4, 6, m7, M9, m11, m13, 14, 17, m19, m21, M22, + m23, m31, 35, 36, m37, m40, m41', }, Quan => { Name => 'Quan', book => '0', }, }; my @book_list; for my $key (keys %$characters) { my $character = $characters->{$key}; my $name = $character->{Name}; # about 30 more lines of code to munge the data my @novels = split(/, /, $character->{book}); # line 132 in code my $first_book = $novels[0]; my $first_type = $first_book =~ /^M/ ? 'major' : $first_book =~ /^m/ + ? 'mentioned' : undef; # line 134 in code $first_book =~ s/\D//g; # line 135 in code $character->{intro}->{book} = $book_list[$first_book]; # line 136 in + code $character->{intro}->{type} = $first_type; $character->{book} = \@novels; # about 30 more lines of code to munge the data }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1233173]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2019-10-23 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?