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

Hi, I am trying to get this output from the code....but not getting it. But I am trying to get this output.
TOTAL AGE BRANCH 1982-05-08 1983-01-09 2 21 3999999 1 1
#!/usr/local/bin/perl my @array; $array[0] = 'JHON 21 3999999 SCHOOL 1982-05-08'; $array[1] = 'JEFF 21 3999999 SCHOOL 1983-01-09'; my $current = undef; my $prev = undef; my %filtered = (); foreach my $_array (@array){ my @split = split(/\s/, $_array); if (defined $filtered{$split[1]}{$split[4]}) { substr($filtered{$split[1]}{$split[4]},0,1) = (++substr($filte +red{$split[1]},0,1)); } else { $filtered{$split[1]}{$split[4]} = "1\t$split[1]\t$split[2]"; } }

Replies are listed 'Best First'.
Re: HASH PROBLEM
by ikegami (Patriarch) on Jan 20, 2009 at 19:30 UTC

    After adding the very important

    use strict; use warnings;

    I get

    Global symbol "@sorted" requires explicit package name at line 13. Execution aborted due to compilation errors.

    I presumed a missing my @sorted = @array; and got:

    Use of uninitialized value in hash element at line 17. Use of uninitialized value in concatenation (.) or string at line 23. Use of uninitialized value in hash element at line 23. Use of uninitialized value in hash element at line 17. Attempt to use reference as lvalue in substr at line 19. Can't use string ("IASH(0x818b194)") as a HASH ref while "strict refs" + in use at line 19.

    Have you looked at those at all?

Re: HASH PROBLEM
by kennethk (Abbot) on Jan 20, 2009 at 21:16 UTC

    I post this with the expectation this is a pseudonym for green_lakers - if this is incorrect, please excuse my hubris.

    We all have difficulties when learning new skills, and knowledge of Perl is something no one is born with. That is why this site features some very smart people who are happy to provide guidance to the rest of us. Don't worry about having difficulty programming Perl - 99.999% of the world can't do as much as you have. But we do ask that you put in a good deal of work on your end. The people who post on this site are donating their time, and so common courtesy asks that you respect that kindness. Posting code without use strict;use warnings; and then asking for help debugging is silly - perl will tell you where your problems are. The on-line documentation at http://perldoc.perl.org is free and easily searchable. If you are having difficulty with a new data structure, try executing the demo code in the documentation and writing toy programs. If your toy program fails, post that code and ask why.

    In conclusion, if you show us respect by putting a good effort into fixing your own code, we're more than happy to help you during your missteps.

Re: HASH PROBLEM
by hbm (Hermit) on Jan 20, 2009 at 19:29 UTC
    Two glaring problems to start with:
    1. You do foreach my $line (@sorted), and yet you only define @array.
    2. You test for $split[1] and $split[12], and yet each line only has 5 elements, 0 through 4.
Re: HASH PROBLEM
by Bloodnok (Vicar) on Jan 20, 2009 at 19:38 UTC
    It looks like you've updated the node following the comments made by ikegami &/or hbhm - that's all well and good, but when updating a node, please, please, use a form of markup e.g. by introducing an Update section - don't just change the posting - it's an absolute nightmare to work out what the heck has gone on ... possibly hindering your chances of further help.

    A user level that continues to overstate my experience :-))
      sorry about that...i will fix it in future updates
Re: HASH PROBLEM
by ikegami (Patriarch) on Jan 20, 2009 at 22:22 UTC

    hi there, i have fixed my previos errors. can you please take a look now?

    use strict; use warnings;

    is still missing and still reporting an error.

    Update: Me bad, the error was of my own creation. I incorrectly fixed the PerlMonk's code wrapping.

Re: HASH PROBLEM
by graff (Chancellor) on Jan 21, 2009 at 01:53 UTC
    It seems the OP may have been updated at least twice without any attempt to preserve earlier content. The most recent update was probably after ikegami's most recent reply, because when I added "use strict" to the version of the OP code I saw a few minutes ago, it compiled okay, and even ran. (But of course it didn't "do" anything, because the version I saw had no "print" statements.)

    So, when will perlnovice81 stop being such a dimwit and start using the advice that is being contributed for his/her benefit? We can only hope... (Update: My sincerest and most embarrased apologies to perlnovice81 -- based on ikegami's update, I see that I had jumped to some very bad conclusions based on incorrect information, and my original snide tone was totally uncalled for. I do hope that I've helped in some way, in spite of my bad behavior.)

    In the meantime, here's some more advice to be ignored: based on this example of desired output:

    TOTAL AGE BRANCH 1982-05-08 1983-01-09 2 21 3999999 1 1
    together with these examples of expected input:
    $array[0] = 'JHON 21 3999999 SCHOOL 1982-05-08'; $array[1] = 'JEFF 21 3999999 SCHOOL 1983-01-09';
    I going to go out on a limb and guess that if the input contained a couple more entries like this:
    $array[2] = 'JREK 18 1234567 LOUNGE 1980-01-01'; $array[3] = 'FOO 18 3456789 BAR 2000-10-10';
    then the additional output for those entries would look like this:
    TOTAL AGE BRANCH 1980-01-01 1 18 1234567 1 TOTAL AGE BRANCH 2000-10-10 1 18 3456789 1
    That's just a wild-ass guess, because there's no way to be sure what the intended rules really are for output (at least, not until the OP shows up with another stealthy update that might explain the rules).

    Still, if that guess is even a little bit close to the intended idea, then there needs to be a very different data structure for loading and keeping track of the input data. Then again, based on what the OP has told us so far, maybe the problem really does involve just two input records and nothing more.

    Here's a version of the OP code (based on what I saw when I started this reply), with a couple additions and changes that might help:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw/Dumper/; my @array; $array[0] = 'JHON 21 3999999 SCHOOL 1982-05-08'; $array[1] = 'JEFF 21 3999999 SCHOOL 1983-01-09'; my $current = undef; my $prev = undef; my %filtered = (); foreach my $_array (@array){ my @split = split(/\s/, $_array); if (defined $filtered{$split[1]}{$split[4]}) { substr($filtered{$split[1]}{$split[4]},0,1) = (++substr($filte +red{$split[1]},0,1)); } else { $filtered{$split[1]}{$split[4]} = "1\t$split[1]\t$split[2]"; } } print Dumper( \%filtered );
    Again, I don't really know what the intent is supposed to be, but this version at least does something.

    (updated to fix indenting in the "else" block)