Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Printing a reference to the output of map

by sierrathedog04 (Hermit)
on Dec 01, 2001 at 03:18 UTC ( [id://128786]=perlquestion: print w/replies, xml ) Need Help??

sierrathedog04 has asked for the wisdom of the Perl Monks concerning the following question:

get_day_name is a function which returns a string. The map command below works properly if I store its output in a %myHash variable and then print the Dumper of a reference to %myHash.

However I should be able to print the output of map directly. Yet Perl doesn't compile

print Dumper \{map {$_ => get_day_name($_)} <DATA>};
either. Nor does it compile
print Dumper {map {$_ => get_day_name($_)} <DATA>};
Why?

Replies are listed 'Best First'.
Re: Printing a reference to the output of map
by clintp (Curate) on Dec 01, 2001 at 03:28 UTC
    use Data::Dumper; sub get_day_name { "hlaghlag"; } print Dumper { map { $_ => get_day_name($_) } <DATA> }; __END__ foo bar baz
    Compiled, ran, and gave the expected output for me. Perl 5.6.0, i686-linux.

    Care to be more specific about "doesn't compile"?

Re: Printing a reference to the output of map
by Fastolfe (Vicar) on Dec 01, 2001 at 03:29 UTC

    I cannot reproduce your problem in 5.6.1. What doesn't compile about that latter statement? It looks fine logically and works fine on my system (taking liberty with the nature of your function and the data):

    #!/usr/bin/perl use Data::Dumper; sub get_day_name { return "first" if $_[0] =~ /one/; return "second" if $_[0] =~ /two/; return "third" if $_[0] =~ /three/; return; } print Dumper {map {$_ => get_day_name($_)} <DATA>}; __DATA__ one two three
    Results:
    $VAR1 = { 'two ' => 'second', 'three ' => 'third', 'one ' => 'first' };

    You probably want a chomp in your map though to get rid of those newlines.

Re: Printing a reference to the output of map
by mikeB (Friar) on Dec 01, 2001 at 03:25 UTC
    maybe you want
    print Dumper [map {$_ => get_day_name($_)} <DATA>];
    map returns a list. The []'s return a reference to an anonymous array created from that list.

    Update I should have read the question more closely. He does want a hash back. See clintp's response.

      map's returning a list all right: in this case a list of paired elements. It's perfectly acceptable to dump this "list" directly into a hash.
Re: Printing a reference to the output of map
by danboo (Beadle) on Dec 01, 2001 at 03:31 UTC
    What do you mean by doesn't "compile". Is it actually failing the compilation phase and spitting out an error? If so, what is the error? Or just not doing what you expect?

    It seems to work fine for me based on the current information. Are you sure you've actually got <DATA>? Did you already read it all in perhaps, leaving it with no more to read?

    hmmm,

    - danboo

Re: Printing a reference to the output of map
by sierrathedog04 (Hermit) on Dec 01, 2001 at 17:46 UTC
    Thanks everyone. I found the error. It was in the line preceding my call to map.

    Instead of using Perl's // to create a comment I was using /* */ to create a comment, which is gibberish to perl.

      For the record '#' is used for creating comments in perl. Hopefully you figured that out already.

      - danboo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 07:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found