Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: A C-like brain in a Perl-like world

by toma (Vicar)
on Sep 27, 2001 at 11:09 UTC ( [id://115013]=note: print w/replies, xml ) Need Help??


in reply to A C-like brain in a Perl-like world

I agree that hashes are the best approach for merging keyed data. If there is some other reason to continue to use arrays, you can compare different approaches for an array lookup routine.

You will find these lines quite helpful when you put them at the top of your program:

use strict; use warnings; use diagnostics;
The diagnostics provide paragraph-long explanations of many common perl coding errors. For the type of mistakes that I make, the diagnostics are correct about 80% of the time. I think this is an amazingly good percentage.

As premchai21 said, you may need to update your version of perl to get this capability.

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
Re: Re: A C-like brain in a Perl-like world
by cyberscribe (Acolyte) on Sep 28, 2001 at 10:59 UTC

    Please explain "strict" to me. Perl seems anything but strict. Except when it comes to if statements, which MUST involve curly braces:

    if(<cond>){%eval_true%}else{%eval_false%}

    unlike shell script or other, looser constructs that know the next line off an if is %eval_true%.

    I must believe there is a good reason for this that eludes me, as I must believe that Perl is about simplicity and elegance. Yet removing curly braces and getting away with it seems like a good, intuitive thing to do to a compiler/interpreter.

    Pontificate?




    p.s. I would like to propose a new tag for posts and cb ... 'pseudocode' - for formatting code that is really shorthand and differentiating it from stuff between legitimate 'code' tags, which should execute in a Perl environment without errors, or with only errors specifically described in the post. What I wrote above was 'pseudocode.

      In perl you can use if as a statement modifier... Therefore, this is perfectly valid code:
      #!/usr/bin/perl -wT use strict; for my $var (0..9) { print "$var is even\n" if $var%2 == 0; # using if as a statement mo +difier }
      For more on use strict, please see strict.pm.

      -Blake

      Forcing a BLOCK rather than an instruction after an if is because of a simple reason: the "dangling else". It is not apparent in which way if $condition if $more_condition do_something() else do_other() is to be disambiguated; many tradiditional languages arbitrarily choose to bind the else branch to the closest if, but that's not readily apparent from looking at the code - and if you want to bind the else of the first if, you still have to use a BLOCK around the second.

      So Perl forces you to use BLOCKs for clarity, but lets you append an if clause as a statement modifier for when you want brevity: do_something if $condition;.

      <CODE> has more functions than formatting. For pseudocode, you can use the regular <tt></tt> HTML tags.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-03-29 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found