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

Is this code correct

by suvendra123 (Initiate)
on Mar 22, 2021 at 17:22 UTC ( [id://11130147]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Is this code correct
by choroba (Cardinal) on Mar 22, 2021 at 17:36 UTC
    We don't know what the proper value is, you didn't tell us. In your example, the uninitialised variable is $sixth, but you seem to be using it as if it held the proper value.

    If your Perl is recent enough (5.10+), you can use

    $variable //= $default_value;

    which uses the defined-or assignment operator that only assigns the value if the variable doesn't have a defined value.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Is this code correct
by davido (Cardinal) on Mar 22, 2021 at 19:30 UTC

    It's incorrect because $sixth has never been initialized, so will always be undefined at the point you're attempting to assign its value to $a.

    Did you mean this instead?

    my $sixth; if (defined $a) { $sixth = $a; } else { $sixth = 3; }

    That would make more sense. And if it's what you actually intended to type into your question, yes it is correct. The choice of $a as a variable name is terrible though, because it is the same name used in Perl's sort routine, and is exempt from strictures. Choose any name that isn't $a or $b. Even $c would be ok.

    So while my rewrite of your code is correct (assuming it was your original intent and you just wrote it wrong for your question), it's not the most Perlish. In Perl we would do something like this:

    my $sixth = defined $a ? $a : 3; # or this my $sixth = $a // 3;

    Dave

Re: Is this code correct
by linuxer (Curate) on Mar 22, 2021 at 18:03 UTC

    Basically that code looks ok.

    As it uses an if/else the ternary operator came to my mind to write it differently:

    my $sixth = 'whatever'; $var = defined($var) ? $sixth : 3;

    As I personally avoid to use variables $a and $b in normal code (only in sort), I used $var here.

Re: Is this code correct
by LanX (Saint) on Mar 22, 2021 at 17:34 UTC
    > How to assign an uninitialized variable with a proper value ?

    you probably(?) want $a //= $sixth;

    edit

    i.e. $a = $sixth if not defined $a;

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Is this code correct
by GrandFather (Saint) on Mar 22, 2021 at 19:56 UTC

    Is it correct?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found