Re: qw vs. q



John Smith wrote:
I found the following example on CPAN:

my $key = 'welcome';
my %data = (
'this' => qw(that),
'tom' => qw(and jerry),
'welcome' => q(Hello World),
'zip' => q(welcome),
);
my @data = keys %data;

my question is:
1) what is the difference between qw and q?

Here is a quote about "qw" from perldoc perlop:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
++ qw/STRING/
++ Evaluates to a list of the words extracted out of
++ STRING, using embedded whitespace as the
++ word delimiters. It can be understood as being
++ roughly equivalent to:
++
++ split(' ', q/STRING/);
++
++ the differences being that it generates a real list
++ at compile time, and in scalar context it returns
++ the last element in the list. So this expression:
++
++ qw(foo bar baz)
++
++ is semantically equivalent to the list:
++
++ 'foo', 'bar', 'baz'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Here is a quote about "q" from perldoc perlop:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
++ q/STRING/
++ 'STRING'
++ A single-quoted, literal string. A backslash
++ represents a backslash unless followed by the
++ delimiter or another backslash, in which case the
++ delimiter or backslash is interpolated.
++
++ $foo = q!I said, "You said, 'She said it.'"!;
++ $bar = q('This is it.');
++ $baz = '\n'; # a two-character string
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

2) why is qw used in first 2 key-element; and q used in last 2
key-element?

To understand what's going on, one needs to look also at the Comma
Operator "=>":

Here is a quote about "=>" from perldoc perlop:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
++ Comma Operator
++ Binary "," is the comma operator. In scalar
++ context it evaluates its left argument, throws
++ that value away, then evaluates its right
++ argument and returns that value. This is just
++ like C's comma operator.
++
++ In list context, it's just the list argument
++ separator, and inserts both its arguments into
++ the list.
++
++ The "=>" operator is a synonym for the comma,
++ but forces any word (consisting entirely of word
++ characters) to its left to be interpreted as a string
++ (as of 5.001). If the argument on the left is not
++ a word, it is first interpreted as an expression,
++ and then the string value of that is used.
++
++ The "=>" operator is helpful in documenting the
++ correspondence between keys and values in
++ hashes, and other paired elements in lists.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

So, the example basically says:
my %data = ('this', 'that', 'tom', 'and', 'jerry',
'welcome', 'Hello World', 'zip', 'welcome');

That could equally be written as:
my %data = (
'this' => 'that',
'tom' => 'and',
'jerry' => 'welcome',
'Hello World' => 'zip',
'welcome'
);

Note that the number of elements in the list is not even, so you will
get a warning "Odd number of elements in hash assignment" when you run
this example.

.



Relevant Pages

  • Re: Why are tuples immutable?
    ... Nothing forces me to have two lists as equal just because there elements ... Any time I want to use the dict elsewhere, ... > pass not only the dict itself, but a list of keys to the dict. ... > memory leaks in C aren't a problem because you've told programmers to ...
    (comp.lang.python)
  • Re: keyboard shortcuts for mose buttons?
    ... folders or files in windows explorer, pressing the ENTER key will open the ... the "mouse keys" functionality is of little use. ... their own shortcuts - separate from Windows. ... Print out lists for those things you are likely to use often. ...
    (microsoft.public.windowsxp.accessibility)
  • Re: Standard DBI Proposal
    ... Tcl has dicts, keyed lists, and arrays, all of which have a natural representation of interleaved keys and values. ... I've made good use of writing textual SQL queries out to a file to later be piped into a SQL engine. ... Ought to have a way to inject raw SQL into the connection ...
    (comp.lang.tcl)
  • Re: Newbie help with array handling
    ... The problem is that Python doesn't have a built-in sorted dictionary ... then you can just use a list of lists ... If you need the dict characteristics a lot, then you may use a dict ... (but keys must be hashable objects): ...
    (comp.lang.python)
  • Re: Text Messages in Chinese and Japanese (WAS: Writing French without accented characters)
    ... > characters in text messages. ... > characters for the string of pinyin text I input using the digit keys ... > "occidental phones". ... Once the appropriate key is entered, lists of kanji would ...
    (sci.lang)