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

I have an array and I am trying to apply a hash to it. This is what I have that works:

if ( -s "/tmp/$strHostname/polling.txt") { my @ndmpselect; open ($fh8, "/tmp/$strHostname/polling.txt"); chomp (@dmpselect = (<$fh8>)); my %ndmphash; for (@dmpselect) { unless (exists $dmphash{$_}) { my $file = "/tmp/loc/" . $_ . '.txt'; open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: +$!"; $dmphash{$_} = $fh; } } }

This works fine. At this point it places a blank file ending in .txt in the /tmp/loc directory. However, I am trying to rewrite this so instead of using the polling.txt file the script can just use an array. However, when I try like this, without reading from the polling.txt file, there is not even a blank file created in the /tmp/loc directory. I am probably missing something simple. Can anyone help?

my @list = (@lines); foreach (@lines) { my @fields = split /,/; if ( $fields[2] eq '1' || $fields[2] eq '0' ) { #@arr1 = do {"@fields[4]\n"}; push @list, $fields[4]; @list =uniq @list; print "$_\n" for @list; for (@list) { my %dmphash; unless (exists $dmphash{$_}) { my $file = "/tmp/loc/" . $_ . '.txt'; open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: +$!"; $dmphash{$_} = $fh; }

Replies are listed 'Best First'.
Re: need to create blank file in directory
by hippo (Archbishop) on Mar 30, 2017 at 17:54 UTC
    for (@list) { my %dmphash; unless (exists $dmphash{$_})

    You test the existence of a key in the hash on the very line after you instantiate the hash so there's no way that key will ever exist (so the test always fails and your file is never opened). Nope, wrong logic, ignore this.

    BTW that's quite some indentation scheme you have going on there. It might help the clarity of your code to run it through perltidy. However, if I attempt to do that it quite rightly complains about the unbalanced braces. Perhaps an SSCCE would make it clearer?

Re: need to create blank file in directory
by 1nickt (Canon) on Mar 30, 2017 at 18:34 UTC

    If you are going through the loop, it's either creating the file or throwing an error for each element. You didn't mention an error; only that the file is not created. This suggests that there is nothing in the loop. What is the output of the line before the loop that prints the elements of @list ?

    Also, as hippo pointed out, you are initializing the hash inside the loop so there is no way any key can exist in it, and it won;t be available outside the for loop.

    Maybe take a step back and describe what you are generally trying to accomplish - there may be a better way. For example, you probably should use Text::CSV for reading in comma-separated data.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: need to create blank file in directory
by poj (Abbot) on Mar 30, 2017 at 19:13 UTC

    If you want unique values think of hash keys

    #!perl use strict; my @lines = ( ',,1,,one', ',,0,,zero' , ',,2,,two' ); # parse lines my %uniq = (); foreach (@lines) { my @fields = split /,/; if ( ($fields[2] eq '1') or ($fields[2] eq '0') ) { ++$uniq{$fields[4]}; } } print "$_\n" for sort keys %uniq; # create fh my $dir = '/tmp/loc/'; my %dmphash = (); for (keys %uniq){ my $file = $dir."$_.txt"; open my $fh, '>', $file or die "Cannot open file '$file' for writing: $!"; $dmphash{$_} = $fh; }
    poj