On Friday, October 17, 2003, at 10:15 AM, Kevin Old wrote:

Help with my script AND Comedy!  Folks, don't forget to tip your
waitress... <grin>

Thank ya. I'll be here all week. :P


On Thu, 2003-10-16 at 17:53, James Edward Gray II wrote:

Okay, first, just have to ask this question:  Are we doing this in the
best way we possibly can?  What about something like this?

my @data;
my @headers = split /;/, scalar <DATAFILE>;
while (<DATAFILE>) {
        chomp;
        my @fields = split /;/, $_;
        foreach (@fields) { s/^\s+//; s/\s+$//; }
        push @data, { map { ($header[$_], $fields[$_]) } 0..$#headers };
}

print Dumper([EMAIL PROTECTED]); # so you can see what it makes...


Not sure I got your point. The data didn't have the look I needed.

You're right, it didn't. That was just my way of giving some other ideas of possibly useful ways to look at that kind of data. I was just making sure you knew what you wanted, which you clearly do. I find your quadrant divided array a little hard to follow, which doesn't matter at all, but I was just making sure there wasn't an easier way. Feel free to ignore me, it works fine for my mother.


Yes, this is almost what I need, but instead of the headers being in the
first column, I need the in the first row.

Add a:


<DATAFILE>;

before the while loop to throw away the headers. I think that's what you meant.

Here's an example I put together to build the first quad.
open (DATAFILE, "mthsum") or die "Can't open file $!\n";

my @tmp = ();
my $i = 0;
while(<DATAFILE>) {
        chomp;
        my $j = 0;
        my @fields = split /;/, $_;
        foreach (@fields) { s/^\s+//; s/\s+$//; }
        #push @tmp, [ @fields[0..7] ];
        foreach ( @fields[ 0..7 ] ) {  # any idea
                $tmp[$j][$i] = $_;     # if there's a better
                $j++;                  # way to write this
        }                              # maybe without the $i and $j
        $i++;                          # or as a push ???
}

print "$tmp[2][0]\n"; # result is STD REPL SHIPPED

Egad, scratch what I said before because that's obviously not what you meant! You like pain don't you man? <laughs>


Alright, let's plan this out a little. You have a piece of working code above. Good start. By the way, don't sweat making two variables, if it works, it works. You might call them $x and $y though, to make it a little easier to follow.

Back to our plan. You ARE making the first quadrant correctly. Now if we put that in the @{ $quads{quad1} } from my example, we're a fourth of the way there right? We could rework the above loop to add them there instead or if we're truly lazy we could add a:

@{ $quads{quad1} } = @tmp;

just after the loop, eh?

Now for the other three quads. Could you whip up loops for them too? I suspect you could since it's pretty much the exact same thing. Then if we load them into the appropriate arrays from my example too, my "prefect" code below finishes up the problem, right? How's that?

Aside: Is this the perfect way to code it up? Probably not. If you do it just as I said, you'll have to read the data four times, which won't be super zippy. However it IS using what we know and it IS a functional solution from a simple plan, right? Don't underestimate that. Once you have a solution, you could work on combining the loops, right? You give that version to management with a speech about "a four times increase in efficiency" and "your Cyber Ninja coding skills" are you're a bloody hero. What's not to like about that? It's a short step from there to being a hit with the ladies, we hope, and then... Oh, you get the idea. ;)

Now, if that sounds adventurous, skip this next chunk of code and have at it Cyber Ninja! If instead you're thinking, "Is this guy ever going to solve my freakin' problem or what?", read on...

my %quads = { quad1 => [ ],
                        quad2 => [ ],
                        quad3 => [ ],
                        quad4 => [ ] };
while (<DATAFILE>) {
        chomp;
        my @fields = split /;/, $_;
        foreach (@fields) { s/^\s+//; s/\s+$//; }
        my @quad1 = @fields[0..7];
        my @quad2 = @fields[8..14]
        my @quad3 = @fields[0, 8..14];
        my @quad4 = @fields[15..18];
        # I'm not ashamed of using many variables to save my sanity!
        if (not @{ $quads{quad1} }) {
                @{ $quads{quad1} } } = map { [ $_ ] } @quad1;
                @{ $quads{quad2} } } = map { [ $_ ] } @quad2;
                @{ $quads{quad3} } } = map { [ $_ ] } @quad3;
                @{ $quads{quad4} } } = map { [ $_ ] } @quad4;
        }
        else {
                push @$_, shift @quad1 foreach @{ $quads{quad1} } };
                push @$_, shift @quad2 foreach @{ $quads{quad2} } };
                push @$_, shift @quad3 foreach @{ $quads{quad3} } };
                push @$_, shift @quad4 foreach @{ $quads{quad4} } };
        }
        # yes, the above can be simplified a lot, if it bothers you
        # but that is left as an exercise to the reader
}

# or we can keep going to get back to the array you asked for...

my @array_2d = @{ $quads{quad1} };
for (my $i = 0; $i < @array_2d; $i++) {
        push @{ $array_2d[$i] }, undef, @{ $quads{quad2}[$i] };
}
push @array_2d, [ ];
push @array_2d,
                map { [ @{ $quads{quad3}[$_] }, undef, @{ $quads{quad4}[$_] } ] }
                0..$#{ $quads{quad3} };

print Dumper([EMAIL PROTECTED]); # I believe that gets us back to where you

Yes, this part is perfect.

Damn, I overshot. I was aiming for just good enough. <laughs>


James, again I sincerely appreciate your help with this script and am
once again awed by the open source community and the lengths some will
go to help others, no matter how complex and confusing the situation is.


Thanks so very much!

Anytime. Perl's community is one of its great advantages, in my opinion. People on this list helped me so much when I was just getting started, I'm glad I can sometimes do the same. Your time will come... ;)


James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to