Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

the answer to an 'uninitialized value, now how do I implement it?

by Bismark (Scribe)
on Jan 18, 2003 at 02:20 UTC ( [id://227899]=perlquestion: print w/replies, xml ) Need Help??

Bismark has asked for the wisdom of the Perl Monks concerning the following question:

In the snippet below I have the dreaded "uninitialized value" I see the cause. $searchresult is initialized with three values, then when I try to use it in the print statement I try to give it seven values, 3 - 7 are the uninitialized culprits. I eventually want to have more than seven fields, so how do I initialize them in the beginning?
my $searchresult = search( part => $part, rev => $rev, filename => './file.db' ); print $searchresult ? "Located Part Number: @{$searchresult}[0 .. 7]\n"

Replies are listed 'Best First'.
Re: the answer to an 'uninitialized value, now how do I implement it?
by pfaut (Priest) on Jan 18, 2003 at 02:28 UTC
    my $searchresult = search( ... ); if ($searchresult) { my $count = $#$searchresult; $count = 7 if $count > 7; print "Located Part Numer: @{$searchresult}[0..$count]\n"; }
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: the answer to an 'uninitialized value, now how do I implement it?
by runrig (Abbot) on Jan 18, 2003 at 02:37 UTC
    While declaring the array in the function, or before returning the result from the function, you can initialize the values if you want to:
    # Before doing stuff with array my @array = ('') x 8; # do stuff with array # or before returning from function for (@array) { $_ = '' unless defined } #or #for (@array[0..7]) { $_ = '' unless defined } return @array;
Re: the answer to an 'uninitialized value, now how do I implement it?
by graff (Chancellor) on Jan 18, 2003 at 02:33 UTC
    Try this:
    my $resultsize = @$searchresult; print "Located Part Number: @$searchresult[0..$resultsize]\n" if ( $resultsize );
    (I was puzzled at what appeared to be an incomplete ternary operator in your code -- there was an expression, a question mark, another expression, then nothing else, when there should have been a colon and a third expression.)
      There is a colon and third expression, I left it out here since it is not involved with the error. I tried your suggestion and still get the uninitialized value error. After the error message it does print all the fields. So it is closer than I have gotten so far.
Re: the answer to an 'uninitialized value, now how do I implement it?
by Coruscate (Sexton) on Jan 18, 2003 at 04:46 UTC

    psst... just for the record, you are printing out an array slice of 8 items, not 7. :) Always remember that arrays begin with the number 0. So when you say @{$var}[0 .. 7], you are retrieving data from the array "slots" 0, 1, 2, 3, 4, 5, 6 and 7, making a total of 8 items.

    You know... just for the record :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://227899]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found