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

I have this part of a script that calls a hash to apply to listoffiles. What it does it take the file mon2a and read the input and use the hash to organize it into a file that goes into /tmp/host. The file looks like this: CORP-INV-STAGE-NDM.txt It works just fine. I get a file that has /vol/volname,jobid in it when I cat CORP-INV-STAGE-NDM.txt. The problem is, I am trying to figure out how to use an array instead of a file like /tmp/mon2a. I need to know the format of how I should write the script so that it would use an array as input. At this point, it works only if I use the file. I've tried a foreach with an array but that doesn't seem to work. I do not need an answer that would make it run properly right away just a suggestion on how to get rid of

open my $fh1, '<', "/tmp/host/mon2a" or die "unable to open file 'fil +e' for reading : $!";

And the syntax of how to replace it with an array that contains the same information.

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; @listx = "$_\n" for @list; print "LETING US SEE @listx"; } } @listx; my %ndmhash; my @listoffiles = ("CORP-INV-STAGE-NDM"); for (@listoffiles) { { my $file = "/tmp/host/" . $_ . '.txt'; open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: +$!"; my $name2 = $file; #$name2 =~ s/\CORP//g; #$name2 =~ s/*CORP-/ /g; $name2 =~ s/'$//; rename($file, $name2); $ndmhash{$_} = $fh; } } my $pattern = join '|', @listoffiles; $pattern = qr/$pattern/; open my $fh1, '<', "/tmp/host/mon2a" or die "unable to op +en file 'file' for reading : $!"; while (<$fh1>) { chomp; my @fields = split(',', $_); local $" = ','; if (($fields[2] eq '1' || $fields[2] eq '0') && /$pattern/) { print { $ndmhash{$fields[4]} } $fields[0], ',' +, $fields[32], ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && !/SM3/ && !/FISH +/; print { $ndmhash{$fields[4]} } $fields[0], ',' +, $fields[34], ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && /BASKET/; print { $ndmhash{$fields[4]} } $fields[0], ',' +, $fields[35], ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && /FORM/; } } { close $ndmhash{$_} or die "Cannot close file '$_': $!"; }

Replies are listed 'Best First'.
Re: using an array in place of a file when calling a hash
by huck (Prior) on Apr 25, 2017 at 19:08 UTC

    ok, try this put this at the top of the script;

    open my $fh1, '<', "/tmp/host/mon2a" or die "unable to open file 'fi +le' for reading : $!"; my @fromfile=<$fh1>; close $fh1;
    then change
    open my $fh1, '<', "/tmp/host/mon2a" or die "unable to open file 'file +' for reading : $!"; while (<$fh1>)
    to
    for (@fromfile)
    This code also suffers from other problems, use strict; use warnings; would help a lot.

    Untested, YMMV

Re: using an array in place of a file when calling a hash
by perldigious (Priest) on Apr 25, 2017 at 19:09 UTC

    I'm not completely clear on your question. I don't see how you can eliminate the need to open the file(s) and read in your data unless you explicitly hard-copy the data from your file(s) in to your script, say into a __DATA__ section. You could of course, open a file, read it in to an array (each element a line from the file), close the file, and then process your data from that array, but it seems like you are asking how to read the data out of a file without having to open the file...

    open my $fh1, '<', "/tmp/host/mon2a" or die "unable to open file 'file +' for reading : $!"; my @lines = <$fh1>; close $fh1;

    Just another Perl hooker - Working on the corner... corner conditions that is.
Re: using an array in place of a file when calling a hash
by Anonymous Monk on Apr 25, 2017 at 21:30 UTC
    Step 1. Run thru perltidy:
    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; @listx = "$_\n" for @list; print "LETING US SEE @listx"; } } @listx; my %ndmhash; my @listoffiles = ("CORP-INV-STAGE-NDM"); for (@listoffiles) { { my $file = "https://server8.kproxy.com/servlet/redirect.srv/slxv/sbuyazzgug/p1/tm +p/host/" . $_ . '.txt'; open( my $fh, '>', $file ) or die "Cannot open file '$file' for writing: $!"; my $name2 = $file; #$name2 =~ s/\CORP//g; #$name2 =~ s/*CORP-/ /g; $name2 =~ s/'$//; rename( $file, $name2 ); $ndmhash{$_} = $fh; } } my $pattern = join '|', @listoffiles; $pattern = qr/$pattern/; open my $fh1, '<', "/tmp/host/mon2a" or die "unable to open file 'file' for reading : $!"; while (<$fh1>) { chomp; my @fields = split( ',', $_ ); local $" = ','; if ( ( $fields[2] eq '1' || $fields[2] eq '0' ) && /$pattern/ ) { print { $ndmhash{ $fields[4] } } $fields[0], ',', $fields[32], + ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && !/SM3/ && !/FISH/; print { $ndmhash{ $fields[4] } } $fields[0], ',', $fields[34], + ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && /BASKET/; print { $ndmhash{ $fields[4] } } $fields[0], ',', $fields[35], + ',', $fields[6], "\n" if /MONTHLY/ && !/,-,/ && /FORM/; } } { close $ndmhash{$_} or die "Cannot close file '$_': $!"; }
    What you posted is incredibly hard to read and quite frankly you are a mean person for doing that. Bad person. No. No. Have you no decency?