Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Re: Hash slices ?

by davorg (Chancellor)
on Dec 01, 2000 at 17:53 UTC ( #44343=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Hash slices ?
in thread Hash slices ?

Any one further investigation, it seems that my original solution suffered from a fundamental problem.

my %hash = (one => 1, two => 2, three => 3); my @keys = qw(four five six); my @slice = @hash{@keys};

@slice is now contains four undef elements, where I thought it would be an empty list (don't know why I thought that - it was very early in the morning.

If the hash slice was interpreted as an array, it would therefore always be true and therefore doesn't solve ChOas' original problem. That would be better addressed using something like:

if (grep { exists %hash{$_} } @keys) { print "yep\n"; } else { print "nope\n"; }
--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: Re: Re: Hash slices ?
by snax (Friar) on Dec 01, 2000 at 18:03 UTC
    perl -w -e '%x=(a=>7,b=>8,c=>9);@s=(qq(a),qq(b));$y=@x{@s};print $y;'
    returns 8 -- so it is pretty clearly the case that a hash slice is properly a list.

Re: Re: Re: Re: Hash slices ?
by ChOas (Curate) on Dec 01, 2000 at 18:09 UTC
    I was thinking about doing it like this to avoid the grep:
    sub Match{foreach (@keys) {return 1 if exists $hash{$_};}return 0} if ( Match ) { print "yep\n"; } else { print "nope\n"; };
    But I was thinking about that already before I thought of
    the slice ;))

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://44343]
help
Chatterbox?
[Yaerox]: I feel like i'm not seeing the forest for the trees. Can someone give me a hint how to research converting all kinds of files into ansi? If origin file is utf8 convert to ansi, is origin file is ansi then skip file.
[1nickt]: Yaerox probably best to whip up an SSCCE demonstrating the failure in what you have now and post to SoPW as a question.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-03-28 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (331 votes). Check out past polls.