Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Hash checking

by kyle (Abbot)
on Apr 25, 2007 at 19:20 UTC ( #612077=note: print w/replies, xml ) Need Help??

in reply to Hash checking

Each line you read in during your input loop has a newline character at the end. Use chomp to take it off.

while(<DATA>){ chomp; my $line = $_; $faclist{$line} = ""; }

After that, I think it should work.

Update: I notice also that this line:

if exists $faclist{$i}{#THIS IS WHERE THE PROBLEM IS

...has a syntax error. It should be:

if ( exists $faclist{$i} ) {#THIS IS WHERE THE PROBLEM IS

Note the parentheses.

Replies are listed 'Best First'.
Re^2: Hash checking
by shigetsu (Hermit) on Apr 25, 2007 at 19:38 UTC

    After that, I think it should work.

    I disagree (I may be wrong). Consider exists:

    Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.

      I think maybe we're misunderstanding each other somehow. I think the reason the OP's search loop is not finding anything in the array is because each of the array keys has an extraneous newline at the end. Take that off (with chomp), and it should be fine.

      use Test::More 'tests' => 4; my $line; my %faclist; $line = "17\n"; $faclist{$line} = ""; $line = "23\n"; chomp $line; $faclist{$line} = ""; ok( ! exists $faclist{17}, 'number 17 does not exist' ); ok( ! exists $faclist{'17'}, 'string 17 does not exist' ); ok( exists $faclist{23}, 'number 23 exists' ); ok( exists $faclist{'23'}, 'string 23 exists' );

        I think maybe we're misunderstanding each other somehow.

        Yes, indeed we did. I'm inclined to say I misunderstood the intent that the Anonymous Monk was trying to put in effect.

        To elaborate a bit: I thought he wanted to clear the hash element and have exists yield failure when applied to the peculiar hash element; thus all the explanatory fuss.

        You did otherwise and were obviously right according to his follow-ups.

Re^2: Hash checking
by Anonymous Monk on Apr 25, 2007 at 19:33 UTC
    Thank you kyle, that helped a lot. Now, another problem has appeared.

    I need to check for the presence of some values in the hash. For example, I am sure tha 1090 is in the hash. But the following code never evaluates as true and thus never does what's inside of the if statement.

    for ($i=1;$i<5001;$i++){
    if (exists $faclist{$i}){
    print "$i\n";
      just how sure are you, and how are you sure?
      use Data::Dumper; print Dumper(\%faclist);

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-09-28 18:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found