[buug] Perl question

Joseph Zitt jzitt at josephzitt.com
Thu Oct 16 16:23:43 PDT 2003


On Thu, 2003-10-16 at 03:09, Brian Dessent wrote:
> Joseph Zitt wrote:
> 
> ($email,$itemcategory,$subject,$adtype,$picurl,$adindex,$descrip,$datenum)
> = split(/###/,$pre);
> > 
> > This list appears 39 times throughout the code, and, once I change it to
> > appropriate fields, I'd like to just deal with it once and refer to it
> > the other times, rather than having 39 unrelated places for it to break.
> > Is there a good way to do this?

[snip]

> The second (and probably better) way would be to just use an associative
> array (hash):
> 
> sub parse_fields {
>   my $pre = shift;
>   my @fields = split(/###/, $pre);
>   my $href = {};
> 
>   $href->{$_} = shift @fields foreach( qw{email itemcategory subject
> adtype picurl adindex descrip datenum} );
>   
>   return $href;
> }
> 
> and then
> 
> my $foo = parse_fields($pre);
> print "email address is $foo->{email}, subject is $foo->{subject}";

Ah! This is just the thing I was looking for. Thanks! I had a feeling
that it would involve a hash, but that "$href->{$_}" syntax was eluding
me.




More information about the buug mailing list