Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: String manipulation

by Ovid (Cardinal)
on Aug 01, 2004 at 13:48 UTC ( #379068=note: print w/replies, xml ) Need Help??

in reply to String manipulation

Elegance, of course, is in the eye of the beholder.

my $init = 1; my $step = 4; my $count = 5; + my $string = join ' ' => map { $_ * $step - ($step - $init) } 1 .. $count;

If your code works, there's nothing wrong with it.


New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: String manipulation
by hotshot (Prior) on Aug 01, 2004 at 13:57 UTC
    My code is working, but what about effieciency and performance, is there a difference between the for loop in my suggestion to map?

      You'll find many nodes on the topic of performance optimization. Let me summarize all of them for you: don't optimize unless you need to.

      One of the biggest issues programmers face (myself included) is the tendency to say "this won't run fast so I had better speed it up." The reality is, if it runs fast enough, it runs fast enough. Particularly as a system gets large, it becomes more and more difficult to guess what will really speed things up. Even if you speed up a loop 100 times, if the program only spends one percent of its time there you've likely wasted your effort.

      But optimizing isn't really bad, is it? I mean, if I optimize everything won't my program will run really fast?

      The answer to that is a resounding NO! I can't recall offhand who said it, but one telling quote is "it's easier to optimize correct code than to correct optimized code." Every hoop you jump through to squeeze out every little bit of performance is another chance for your foot to catch on that hoop and make you stumble. You'll likely introduce more bugs and you'll simply waste time trying to second guess whether or not a particular construct can be faster when what you really want to be doing is delivering product. You'll also be more likely to obfuscate your code and make maintenance more difficult. Only when you have a deliverable and you can get an idea how it's likely to be used in a production environment can you really begin to start performance profiling to find out where the bottlenecks, if any, are.

      In summary, do not fall into the time-wasting trap of second guessing the performance of your code. Make your code correct and only when you really have a performance problem should you start profiling things. With a performance profile in hand, then you can know where you should be optimizing instead of guessing.

      (Note: the above is good general advice and there are definitely times it should be ignored, but once you get to the point where you can make the distinction you'll be giving others this advice :)


      New address of my CGI Course.

        Wow, now's one of those rare times when I wish I could double-vote on a node.

        I'd say that optimization should be really, really rare. Correctness is *so* much more important that it really should get almost exclusive attention. If for example, you have a really horrifically borked query due to bad DB indexes, I'd suggest that you're facing brokenness not optimization at that point.

        Think *very* carefully before trading simplicity for performance.

      My code is working

      Try and run it. It does not compile.

      My code is working

      Fix the compiler errors and then fix the logical errors so it produces what you say it should.

      My code is working

      Your code is most certainly not working. Just look at the loop and tell me how many loops you will get for $step = 1000 and numOfMembers = 1000. I get 1 loop, how about you? Don't see that adding 1000 members......

      for (my $i=2; $i<=$numOfMembers; $i+=$step) {



        sorry, I pasted my code before fixing the iterator, should be $i++ instead of $i+=$step and missing '*' in the formula inside the for loop
Re^2: String manipulation
by bipinbalan (Initiate) on Feb 14, 2008 at 07:40 UTC
      Try this code :
    #!/usr/bin/perl -w use strict; my ($initValue,$step,$numOfMembers,$str) = (2,3,5,''); foreach(1..$numOfMembers) { $str.= (/1/)?$initValue:' '.$initValue; $initValue+=$step; } print "$str\n";
    Cheers !
      Bipin Balan

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2023-06-01 19:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found