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) |