Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

tr doesn't *really* use a list

by Boldra (Deacon)
on Dec 27, 2000 at 22:46 UTC ( #48488=perlquestion: print w/replies, xml ) Need Help??
Boldra has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone convince me that "list" is the correct term for the parameters used by tr? The usage is in perldoc and the camel book, but I don't think it matches the usual usage in perl documentation (where a list is an anonymous array)

if tr really used a list, I'd be able to do something like this:
eval q!tr("\xE4", "\xE9", "\xF6", "\xFC")('ae', 'e', 'oe', 'ue')!;
(Which will probably try to replace " with ' and so forth...)

Can anyone suggest an elegant solution without resorting to four(+) seperate regexes?

Replies are listed 'Best First'.
Re: tr doesn't *really* use a list
by Fastolfe (Vicar) on Dec 27, 2000 at 22:59 UTC
    tr/y is the transliteration operator, and only works on single characters. It does not use a Perl "list" per se, but you give it a bunch of characters, so it can be said to be a 'list' in the common sense.

    Since it only works on single characters, you won't be able to get the one -> two-character mappings like that using it. You'll have to resort to something like this:

    my %MAP = ( "\xE4" => 'ae', "\xE9" => 'e', "\xF6" => 'oe', "\xFC" => 'ue', ); my $mapfrom = join('|', map { quotemeta } keys %MAP); s/($mapfrom)/$MAP{$1}/eg;
(tye)Re: tr doesn't *really* use a list
by tye (Sage) on Dec 27, 2000 at 22:53 UTC

    "list" is not a well-defined term with Perl, it is just an English word. This is confusing to most people because "scalar", "scalar context", and "list context" (as well as "array") are all well-defined Perl terms.

    Different people have proposed their own interpretation of what a "Perl list" is, but they don't really agree very well so you are best to free yourself of this impression.

    tr doesn't take a list of scalars. What word do you suggest be used in place of "list" in the "list of characters" that tr has two of?

            - tye (but my friends call me "Tye")
      tr doesn't take a list of scalars. What word do you suggest be used in place of "list" in the "list of characters" that tr has two of?
      A word that springs to mind and seems to capture the intended meaning is 'set', and perhaps, more correctly: 'ordered set'.
      What word do you suggest be used in place of "list" in the "list of characters" that tr has two of?

      eh... "string"?

        But "string" comes much closer to having a narrow definition in Perl than "list" does so this would make the problem worse. If I said that tr/a-z/A-Z/ replaced the characters in the first string with the cooresponding characters from the second string, then, to me, I'd be saying that "b" is not changed to "B" since "b" does not appear in the string "a-z".

                - tye (but my friends call me "Tye")
Re: tr doesn't *really* use a list
by chipmunk (Parson) on Dec 27, 2000 at 23:20 UTC
    I would say that tr's "parameters" are more like a character class than a list. In particular, you can write a-z to mean all the letters from a to z. Calling it a character class, however, might suggest that it supports \w, \d, \s, etc.; unfortunately those are only available in the regular expression operators.

    On the other hand, it is certainly inaccurate to say that a list in Perl is an anonymous array. Although this code features a list:   print "Hello ", $name, ", how are you?";
    it does not feature an anonymous array.

    I would say that a list in Perl is any grouping of scalars (including groupings of zero or one scalar) on the stack. An array is a list of scalars for which memory has been allocated especially (outside the stack), and an anonymous array is an array where there is no symbol name that refers to the array's memory.

      I would say that a list in Perl is any grouping of scalars (including groupings of zero or one scalar) on the stack.

      Then ($a,$b,$c) isn't a list. It might produce a "list" (using your definition), if used in a list context.

      I certainly consider ($a,$b,$c) a list. It is a list of scalar expressions which has everything to do with the syntax of the construct and nothing to do with some Perl data structure which certainly exists (in multiple forms) but that people keep wanting to say is the one true meaning of "list" when dealing with Perl.

      "List" can mean a list of scalars in memory, much like a Perl array, but it can also mean many other things. I find people repeatedly getting into trouble due to attempts to nail down the definition of a "Perl list".

      There is no one definition of a "Perl list".

      Update: and the reason this leads to trouble is that even if we restrict ourselves to the standard documentation that comes with Perl, the word "list" is used to mean many different things in that documentation. Sometimes the word "list" is used to mean different things by the same author in the same paragraph. So if you have a "nailed down" meaning for "list", some part of the Perl documentation will tell you wrong things (like that tr takes a list of scalars).

      Note that I don't think we should "fix" this by only using "list" to mean only one thing in the Perl documentation. I'd hate to see the ton of "Oops, I wrote 'list' but that wasn't a Perl List(tm) I was talking about so I'll have to call that a... uh.. 'set'... no, hmm... 'collection', uh, 'tuple'. Yes 'tuple'!"

              - tye (but my friends call me "Tye")
        Yes, ($a, $b, $c) is a list in that, syntactically, there are some scalars and they are separated by commas. But I don't think that's the interesting sense of 'list' in the context of Perl. In: $x = ($a, $b, $c)
        semantically, ($a, $b, $c) do not constitute a list because they are never on the stack together. That's why that code assigns $c to $x, whereas: $x = @a = ($a, $b, $c)
        assigns 3 to $x. It is just as easy for someone to get into trouble by not understanding what is meant by a Perl list.

        In the documentation, the word 'list' certainly appears in places where it does not mean "a grouping of scalars on the stack". I don't think it follows from that, however, that there cannot be a specific meaning for 'list' as shorthand for 'Perl list'.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://48488]
Approved by root
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-06-24 03:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.