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

Fellow monks, I am in need of your assistance.

I'm doing a project where I've set up an anonymous hash to hold information gathered from a regex but coming from multiple lines so it isn't keeping name, addr, etc. with the phone and level information.

Here's my code: foreach $line(@urllist){ $data = get($line); @x = split/\r\n|\n/,$data; foreach my $line1(@x){ ($school->{school}, $school->{addr}, $school->{city}, $school->{zip}) = ($1, $2, $3, $4) if $line1 =~ m!$regex1!io; ($school->{phone}, $school->{level}) += ($1, $2) if $line1 =~ m!$regex2!io; } print "$school->{school}\n$school->{addr}\n$school->{city}\n $school->{zip}\n$school->{phone}\n$school->{level}\n\n"; };

My first thought was to kludge regex2 to have 4 empty values but I now realize that would clobber my other data that I've matched already.

Without concatenating the data lines, or using a subhash on the hash, is there a way I can keep all that data keyed together?

Thanks in advance!

Some people fall from grace. I prefer a running start...

Replies are listed 'Best First'.
Re: Question on anon hashes
by graff (Chancellor) on Aug 21, 2002 at 01:25 UTC
    In your closing request:

    Without concatenating the data lines, or using a subhash on the hash, is there a way I can keep all that data keyed together?

    Is there some reason why you need to avoid concatenating the data lines, or using a multi-level hash? Because I think either of those (actually both) would suit what I think you want... fever's point about joining the regexes seems like a very plausible place to start:

    foreach $line(@urllist){ $data = get($line); if ( $data =~ m!$regex1\r?\n$regex2!io ) { $school->{school} = $1; ($school->{school}{addr}, $school->{school}{city}, $school->{school}{zip}, $school->{school}{phone}, $school->{school}{level}) = ($2,$3,$4,$5,$6); } # might the two lines show up in reverse order? then elsif ( $data =~ m!$regex2\r?\n$regex1!io ) { $school->{school} = $3; ($school->{school}{addr}, $school->{school}{city}, $school->{school}{zip}, $school->{school}{phone}, $school->{school}{level}) = ($4,$5,$6,$1,$2); } else { next } print join $/, $school->{school}, map{$school->{school}{$_}} qw/addr city zip phone level/; print $/; };
    Did I draw the right conclusions about your data? Does this break some condition that you left out of your post? (update: fixed indentation)
      Thanks for that!

      The multi-level hash idea just came to me before I read your post. I don't know why I didn't think of it this afternoon while I was coding this.

      As far as the regex goes, the data is on 2 seperate lines, so I don't think that combining the regex is necessarily the way to go. The multi-level hash idea seems the easiest for this program, keying on the school like you did.

      Thanks again!

      Some people fall from grace. I prefer a running start...

        As far as the regex goes, the data is on 2 seperate lines, so I don't think that combining the regex is necessarily the way to go

        Hmmm. Well, your original code seemed to assume that those two separate lines were somehow stored in a single scalar sting, as in: "$data = get($line)" -- because after $data gets its value from this sub call, you split it with /\r?\n/. (you originally used /\r\n|\n/, which does the same thing.)

        So that's why I suggested ditching the split, and just looking for the combination of your two regexes separated by the line separator that was apparently embedded in $data.

Re: Question on anon hashes
by fglock (Vicar) on Aug 21, 2002 at 01:09 UTC

    You've got to save each school in a different place. One way is to use a data index, like this:

    $school_number = -1; # no schools yet ... foreach my $line1(@x){ $school_number++; # change the data index ($school[$school_number]{school}, $school[$school_number]{addr}, ...
Re: Question on anon hashes
by djantzen (Priest) on Aug 21, 2002 at 00:31 UTC

    What do your regexes look like? Are you sure they couldn't be combined into a single pattern? Also, I don't see an anonymous hash anywhere, just the hash reference $school.