Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

No output to screen or output file

by dkhalfe (Acolyte)
on Jul 24, 2012 at 17:10 UTC ( #983472=perlquestion: print w/ replies, xml ) Need Help??
dkhalfe has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I am stumped and need some assistance. My code:

#variables my $x=0; my @keys; my @holder; my @array_hash; my $blank = "-"; my $numerical_value = "num"; my $string_value = "string"; my $filter = "filter"; my $append = "append"; for(my $c = 0; $c <= $num_elements; $c++) { if('$hash{$filter[$c]->[4]}' eq '$filter') { if (exists($hash{$filter[$c]->[0]})) { print "VALUE EXISTS", "\n"; # continue; if('$hash{$filter[$c]->[3]}' eq '$numerical_value') { #process filtering for numerical value if ((eval "$hash{$filter[$c]->[0]} $filter[$c]->[1 +] $filter[$c]->[2]")) { print OUTFILE $line, "\n"; } } if('$hash{$filter[$c]->[3]}' eq '$string_value') { #process filtering for string value if ((eval "'$hash{$filter[$c]->[0]}' $filter[$c]-> +[1] '$filter[$c]->[2]'")) { print OUTFILE $line, "\n"; } } } else { print "COLUMN NAME DOES NOT EXIST. CHECK FILTER FILE F +OR ANY POSSIBLE ERRORS AND RE-RUN PROGRAM. PROGRAM WILL NOW TERMINATE +.", "\n"; exit 0; } }

I am using a for loop to iterate through the entirety of the array I create when reading in from an input file. As long as  $c is less than or eq to the number of elements in the array -- if the hash of the column name is equal to "filter", continue -- if the hash of the column name exists, continue (else display error and terminate program) -- if the hash of the column name equals 'num' or 'string' (respectively), continue -- if the hash of the column name evaluates as true, print to OUTPUT file.

However, no output is printed to my OUTFILE (note: this is only part of my code, the other part of code relates to storing the information from a filter file and input file. It has been tested and works so I know the problem lays in the above mentioned code. Please help!! Thank you!

Here is the input file so you can make sense of what is stored in the array and what is going on in the eval statement

column relationship value num_or_string filter_or_append order a <= 0.3 num filter 1 b = abc string append 3 c <= 0.3 num filter 2

Note  if ((eval "'$hash{$filter[$c]->[0]}' $filter[$c]->[1] '$filter[$c]->[2]'"))
translates to -- if column a <= 0.3 -- print OUTFILE line.

Comment on No output to screen or output file
Select or Download Code
Re: No output to screen or output file
by Riales (Hermit) on Jul 24, 2012 at 17:15 UTC

    Do you define $num_elements anywhere? I'm thinking you never enter the for loop because $num_elements is undefined.

    Also, you appear to be using $c to iterate over $filter. This seems odd to me because $filter is just a string. Which array are you actually trying to iterate over?

      Yes I do

       my $num_elements = (@filter-1); #Number of elements in array

      I have even printed the $num_elements variable to the screen and it works.

        $num_elements isn't really named correctly. In the code, it is the last index of the array. Just a minor nit.   :-)

        But, your for loop is ok because it tests if $c <= $num_elements.

        Chris

Re: No output to screen or output file
by toolic (Chancellor) on Jul 24, 2012 at 17:18 UTC
    Single quotes prevent variable interpolation. Change:
    if('$hash{$filter[$c]->[4]}' eq '$filter')

    to:

    if($hash{$filter[$c]->[4]} eq $filter)

    This is detected by perlcritic

      Dont I have to format my code with those single quotations when comparing string values? For that reason, I have the program check and differentiate between strings and numerical values (string or num) - I posted a question earlier and the solution to my problem was to include these single quotations. I believe I kept getting an error when trying to run the program without them.

        Here's the quick explanation. What does this piece of code mean?

        my $result = something;

        Is something a function call? Is it a string literal?

        You have to make your intent clear to Perl and to human readers. Make it unambiguously a function call with:

        my $result = something();

        ... or a string with:

        my $result = 'something';

        Where it's not obvious what you mean, make it obvious. That's what strict was complaining about.


        Improve your skills with Modern Perl: the free book.

      Use of uninitialized value in string eq at trial1.pl line 128, <IN> line 1767.

      I removed the single quotations and get the following error. When I put the quotes back in, the program succesfully runs but no output is printed to my output file.

        Sounds like your input file is 1766 lines long and you are trying to read in the next line after you've already reached the end. Can you show the code where you read in your file?
Re: No output to screen or output file
by blue_cowdawg (Prior) on Jul 24, 2012 at 17:34 UTC

    I'm very confused about your coding.

    my $filter = "filter"; # I'm a scalar here..
    and then later in your code:
    $hash{$filter[$c]->[4]} # or am I an array?
    besides which you are putting the above in a pair of single quotes
    '$hash{$filter[$c]->[4]}' # that will literally be "$hash{$filter[$c +]->[4]}"
    not cool.

    Start your script off with use strict; and you'll see helpful information.

    $ ./booboo.pl Global symbol "$num_elements" requires explicit package name at ./boob +oo.pl line 14. Global symbol "%hash" requires explicit package name at ./booboo.pl li +ne 18. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 18. Global symbol "%hash" requires explicit package name at ./booboo.pl li +ne 25. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 25. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 25. syntax error at ./booboo.pl line 26, near "+]" (Might be a runaway multi-line "" string starting on line 25) Global symbol "$line" requires explicit package name at ./booboo.pl li +ne 28. Global symbol "%hash" requires explicit package name at ./booboo.pl li +ne 34. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 34. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 34. Global symbol "@filter" requires explicit package name at ./booboo.pl +line 34. Global symbol "$line" requires explicit package name at ./booboo.pl li +ne 37. Missing right curly or square bracket at ./booboo.pl line 49, at end o +f line ./booboo.pl has too many errors.

    Above is the result of that.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      I noted that I did not include the entirety of my code. I changed my $filter variable to $filter_data. Also I took off the single quotations from my references to the arrays. This has not solved the problem.

        if($hash{$filter[$c]->[4]} eq $filter_data)

      I believe my problem lays with comparing a reference to an array with a variable (containing a string). Any insight on this would be helpful. Thanks!

      Also, whenI remove the quotes and attempt to run the program the following error appears {Also, 'use strict' is included in my code}

      Error: Use of uninitialized value in string eq at trial1.pl line 142, <IN> line 1767.

            Error: Use of uninitialized value in string eq at trial1.pl line 142, <IN> line 1767.

        here is where I'd put in some print statements to see what values are present in the variables being cited by the error code. You've told me that you didn't include all your code, but there are some things you haven't included that are probably germain to the problem at hand...


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        use strict; use warnings; no warnings qw(uninitialized);
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: No output to screen or output file
by Marshall (Prior) on Jul 25, 2012 at 06:44 UTC
    I am having a hard time understanding the spec.

    # field1: I guess a,b,c could be numbers: 0,1,2. If these are columns, then how many columns could there be?

    # field2: some kind of comparison, compare what to what? is this a sort order?

    # field3: I guess either a numeric or alpha-numeric string?

    # field 4:"num" or "string" is redundant and not necessary?

    # field4: I just guess that "filter" is a logical "AND" and that "APPEND" is a logical "OR"? But "and" to what and "or" to what?

    # field5: I guess this is some priority, but to what?

    a <= 0.3 num filter 1 b = abc string append 3 c <= 0.3 num filter 2

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-08-23 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls