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

\This code works while ( $line = <$fh>) { ($store, $quarter, $quarteryear, $dcount, $date, $status) = chompspl +it($line); my $key = $quarter . "," . $quarteryear ; $count{$key}++; } ############################### New code example of what i am trying to do, yes passing string in subr +outine $header = 'Total profit Submitted by Quarter'; $string = '$quarter . "," . $quarteryear' submain($header,$string); $header = 'Total loss Submitted by Quarter'; $string = $'quarter . "," . $quarteryear . "," $status' submain($header,$string); trying to use the string sub submain ($head,$str) { my ($head,$str) = @_; open my $fh, '<', $file or die "Could not open '$file' $!"; while ( $line = <$fh>) { ($store, $quarter, $quarteryear, $dcount, $date, $status) = chompspl +it($line); my $key = $str ; is this even possible, i remeber doing it in some + other language $count{$key}++; print $key; return; } Thanks for the help Michael

Replies are listed 'Best First'.
Re: stuck on code
by GrandFather (Saint) on Apr 01, 2014 at 01:51 UTC
Re: stuck on code
by davido (Cardinal) on Apr 01, 2014 at 07:03 UTC

    The only question I see is this:

    my $key = $str; is this even possible, i remember doing it in some oth +er language

    $str receives a value passed into the subroutine when it is called. So yes, it's possible. But sub submain ($head,$str) isn't. Eliminate the ($head,$str) portion of your subroutine declaration.

    Next, what you're doing makes no sense to me. Why would you pass in a value, $str, and then iterate over each line of a file, and for each line, increment the value of the hash element indexed to the string passed into the sub? And even if your goal is to get a line count (which is what you're almost doing), why bother printing the string passed into the subroutine again and again? Finally, why return from inside of a while() loop, guaranteeing that it will loop only one time, incrementing $count{$key} just once?

    The syntax errors come down to trying to write Perl code without knowing Perl. However, the logic errors seem to be the result of trying to write code without knowing how to program. We are going to have a hard time helping when the only spec we have to work with is code that won't compile, and that even if it did, had logic errors so prominent that it's impossible to determine what the code is actually supposed to do if the errors were all fixed.

    I think the only way you're going to get help on this one that is useful to you is if you post some questions in plain old English that are comprehensible to us.


    Dave

Re: stuck on code
by boftx (Deacon) on Apr 01, 2014 at 01:39 UTC

    I usually refrain from replying with nothing but a compliant about lack of strictures or code that can not run, but ... DUDE!

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.