Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Check if value exists into an array in a hash

by Sombrerero_loco (Beadle)
on Feb 10, 2009 at 15:56 UTC ( [id://742798]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Check if value exists into an array in a hash
by kennethk (Abbot) on Feb 10, 2009 at 16:08 UTC

    Please read Markup in the Monastery as you've just made a mess of my browser window.

    I'm pretty sure you don't want an if-elsif-else set here, unless you want to keep at most one value from each line. You also probably mean to have a hash of hashes of hashes structure here in place of what you are doing (perllol). The test you are performing actually checks if an index exists yet, not if the value is the array. The code you want is likely closer to:

    while (<FICHEROCFG>) { my $datos_cfg = $_; my ($var1,$var2,$var3,$var4) = split (",", $datos_cfg); $db{$var1}{'Entidad'}{$var2} = 1; $db{$var1}{'Agrupacion'}{$var3} = 1; $db{$var1}{'Operativa'}{$var4} = 1; }

    Update: And to answer your updated question, the index of the last element of @array is given by $#array.

Re: Check if value exists into an array in a hash
by pileofrogs (Priest) on Feb 10, 2009 at 18:59 UTC

    If I understand what you're doing, I think, yes, it will do what you want.

    exists($list[$num])

    Will tell if there is an item of some kind, possibly undefined, at index $num in list $list.

    I think $#{$arrayref} is what you're looking for with the 2nd question. EG $#{$hash_of_lists{$foo}}.

      The issue is that push @list, $num doesn't set element $list[$num], so the tests don't correlate. If instead the OP used $list[$num] = 1; or equivalent, then [she|he] would get a false positive with exists if [she|he] set $list[5] and then checked $list[3] - defined would give correct results, though.

        Oy, yes. I see what you mean. The array will grow to accomodate it's highest index, and therefore all lower indexes will come into existence.

        I think the OP should use a hash. Clearly, it's a hash-ish behavior he/she wants. The fact that the keys are numbers is making it look like a list problem, but really, it's a hash problem.

Re: Check if value exists into an array in a hash
by kennethk (Abbot) on Feb 11, 2009 at 16:21 UTC

    Regarding Update 2:

    The problem is that $db{$var1} is an array reference, not a hash reference. Perhaps you mean $db{$var1}[$indice]{counter} = '';?

    As a second note, so long as your hash keys contain only alphanumerics and underscores, Perl will automatically stringify the result - there's no need for quotes.

      thanks for the quotes trick!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found