DISQUS

carlo.comments: carlo.log → unset(Perl)

  • mzehrer · 2 years ago
    You could use grep and do something like:

    my @indices = grep { $array[$_] eq 'foo' } 0 .. $#array;

    which is not very cool if you have large arrays. But who knows what other languages do in the background of their convenience methods?

    You should think about using a hash here. To keep things in order you could use Tie::IxHash.

    But I agree, Perl is very "old school"...
  • Carlo Zottmann · 2 years ago
    Heh. Thanks for the note, Michael.

    > But who knows what other languages do in the background of their convenience methods?

    The question is: should I care? Shouldn't a programming language get out of my way instead and help me to ideally achieve the highest state of productivity?
  • Ben · 2 years ago
    Or take "Array::AsHash" which treats the array as a hash. Then use :
    $i=$array->aindex('foo');

    -Ben
  • Mina · 2 years ago
    Dear coder and tinkerer,

    Try using the right tool for the right job. Irrespective of the language, the fact that you want this out of an array indicates that your data may have been coerced into that 1D structure where a richer structure should have been used.

    As others have mentioned, there are many simple ways in perl to do what you want. However, perl itself is not offering such a method that's ultimately helping you shoot your own foot off.

    Think about it for a bit. What if the array has a huge data set ? What if it had duplicates of what you want ? Do you want the first match, middle match or last match ? do you want to optimize by stopping the search after a match ? Will you re-search the array again for the same key ? For a different key ?

    To answer your question: Yes, absolutely you should care. Hardware is cheap and programmer's time is valuable, but simple understanding of basic optimization and the right modeling for the right problem will make the difference between your application not making a nudge in resource usage vs. large hiccups and needing exponentially more hardware when you grow and scale.
  • Carlo Zottmann · 2 years ago
    > Irrespective of the language, the fact that you want this out of an array indicates that your data may have been coerced into that 1D structure where a richer structure should have been used. [..] Think about it for a bit.

    Actually, I did. I used an array because I have a _very_ limited amount of _unique_ strings, and I don't need anything fancy at all. Using a hash felt like overkill, but I by now believe I was wrong. That said, I don't want to go into more details about what I was trying to do, mostly because (A) of an NDA and (B) it was just a(nother) particular thing that ticked me off.

    A few other pet peeves: for a language that's about text extraction and manipulation, I really miss a built-in `trim()` function of sorts (which rids a string of leading and trailing whitespace). And why is there no `switch()`?

    But to clarify: if I could choose, I would ditch Perl in favor of something that makes more sense (to me). Python comes to (my) mind. Unfortunately, I don't have that choice -- the company policy is to use Perl for this field of backend code, so I have to deal with it.

    EITHER WAY: Thanks for the suggestions.
  • Mike West · 2 years ago
    Trim = `sub trim { my ($string) = @_; $string =~ s/^\s*(.+?)\s*$/$1/; return $string; }`

    switch... that's a little tougher. how about the "cannonical":

    SWITCH: for ($variable) {
    ($variable eq "thing1") && do {
    do_neat_stuff();
    last SWITCH;
    };
    ($variable eq "thing2") && do {
    do_more_neat_stuff();
    last SWITCH;
    };
    ...
    }

    Ah, perl... :)
  • Carlo Zottmann · 2 years ago
    Mike, I know how to write my own `trim()` function. My point was: why isn't something like this part of the core Perl?

    And the `switch`... It might work, but it's fugly and definitely more work than it should be. :P
  • Ivo · 2 years ago
    You can't expect everything to be built-in in a language, especially when it comes to non-standard stuff, like your case. A language has to be smart and everything, but also fast.

    set(Perl) while(1);
  • Carlo Zottmann · 2 years ago
    In _my opinion_, if something is in the core of a language, in this case arrays, then the language as such shouldn't just merely acknowledge its existence -- but offer methods to actually work with it as well.

    Also: trimming a string is something Perl isn't suppose to do in the first place?

    Use Perl all you want, that's cool, but please don't tell me it's elegant.
  • mtm · 2 years ago
    True. PHP has so many functions for hashes, arrays, database access. I wish for Perl you could define a standard set of modules you want to import (use) for every script. I seem to use a set of 5 every single time.

    Oh, since the most elegant (yes, in relative terms of course...) method wasn't mentioned yet:

    use List::MoreUtils;
    my $i = first_index { $_ eq 'abc' } @a_list;
    my $i = first_index { m/abc/ } @a_list;

    http://search.cpan.org/perldoc?List::MoreUtils
  • ysth · 2 years ago
    mtm, that feature was added two years ago:

    http://perldoc.perl.org/perl587delta.html#Optio...
  • ysth · 2 years ago
    Carlo,

    I see your basic complaint as being that arrays, strings, etc. aren't objects with a rich set of methods. This is true, but not actually that big a deal. Given that the
    main types aren't objects, to avoid namespace pollution, Perl defers a lot of functionality to modules. Text::Trim provides trim, ltrim, and rtrim functions.

    With respect to the array index problem, others have suggested modules that
    allow you to have an array interface but also lookup by value. There isn't something that I know of that works on a basic array rather than a tied
    array or an object initialized from an array. I suspect this is because idiomatic
    perl tends to use array indexes very very rarely. In fact, using an array index is usually a big clue that you need to rethink your storage (which it sounds like you've done.)

    I'll finish with this:

    my %array_index_for_value = map $array[$_] => $_, 0..$#array;
  • Carlo Zottmann · 2 years ago
    ysth, many thanks for the suggestion. I'll try it on Monday when I'm back in the office.
  • Mina · 2 years ago
    ysth, for your technical suggestion, a hash slice is actually pretty neat and slightly more readable:

    @names = qw(bob mary jane mark jane steve);
    @indexes{@names} = 0 .. $#names;

    While it may look like "indexes" is an array, it's actually a hash being vivified and populated with a huge slice (the array of names)....

    use Data::Dumper;
    print Dumper \%indexes
  • Programmer · 10 months ago
    Ask your company if they are planning to convert all that legacy Perl to a non-dead language.
  • Nick · 3 months ago
    Hi, I realize this was two years ago, but I had a few comments on it. Others have noted structures for your original problem, but not many for your others.

    One person noted a trim module, but there is also the builtin chomp() function which removes trailing whitespace (no modules needed) - but I believe it only removes one character, so it needs to be run in a loop if it's indefinite.

    As for perl not having switch, try "use Switch;" It's actually a well-done switch setup and I believe it's standard in most perl distributions.

    Mostly though, I think perl is as flexible as you are saying it isn't, but you should check out CPAN before looking to write your own code. I have frequently started to write large blobs of code, only to search CPAN and fine that a more robust and elegant solution is available. I now tend to search it before doing any wacky programming.