Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: assign an array into hash

by GrandFather (Saint)
on Apr 26, 2007 at 09:31 UTC ( [id://612169]=note: print w/replies, xml ) Need Help??


in reply to assign an array into hash

qw(@con) is an error. Perl would have told you about it if you used strictures (use strict; use warnings;). Always use strictures. qw quotes 'words' where in this case a 'word' is any sequence of non-white space characters. @con is taken to be a word by qw so you are trying to assign a single element list to a hash - that doesn't do what you want.

Consider instead:

use strict; use warnings; my %con; while (<DATA>) { chomp($_); next unless /^(.*?)=>(.*)/; $con{$1} = $2; } print "Key: $_, Value: $con{$_}\n" for sort keys %con; __DATA__ article=>art chapter=>chap section=>sec

Prints:

Key: article, Value: art Key: chapter, Value: chap Key: section, Value: sec

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found