in reply to Building an SQL query

My apologies for posting a somewhat ambiguous question. I guess you can say my confusion more or less lies within the realm of what to do after I fetch all the data in a particular addresses column. It would definitely be easier if there were two different tables, one for username, and one for all the corresponding name and address values, but for now, lets assume that this is the way it has to be. Okay, a quick example: Lets say a user, named "Blah" wants to edit a particular address ( Joe Schmoe joe@scmoe.com ). How can I pull this peice of data out of all the addresses If I select * from addresses?

Replies are listed 'Best First'.
RE: Re: Building an SQL query
by swiftone (Curate) on Oct 19, 2000 at 21:44 UTC
    Okay, so your question is basically "How do I edit a string of comma delimited email addresses"?

    One way is to separate the data. Split the string on commas (assumes no email address have commas in them), then split each of those elements into outside <> and inside <>
    Edit the data, then rebuild your string.

    This seems to work:

    #!/usr/bin/perl -w use strict; my $test='A <a@bob.com>, B <B@b.org>, Calcutta <O@thehorror.net>'; my @names = split /\s?,\s?/, $test; #split apart $_ = [split /\s(?=<)/, $_] for (@names); #make array refs of each # Above happily learned from merlyn foreach my $name (@names) { foreach (@{$name}){ print "--$_--"; } print "\n"; }
    It outputs:
    --A----<a@bob.com>-- --B----<B@b.org>-- --Calcutta----<O@thehorror.net>--

    And of course, you can edit that as you wish. Of course, if you KNOW one element, it'd be better to use a hash

    #!/usr/bin/perl -w use strict; my $test='A <a@bob.com>, B <B@b.org>, Calcutta <O@thehorror.net>'; my %names = split /,\s|\s(?=<)/, $test; #split apart foreach my $name (keys (%names)) { print "$name==$names{$name}\n"; }
    This also works, and allows you to access (and modify) $names{Joe Schmoe}

    Rebuilding your original list is even easier after you modify it.

    my @names; while (my ($key, $value) = each %names){ push @names, "$key $value"; } print join ', ',@names;
    Note that all of the above code assumes that:
    1. No email addresses or names have commas
    2. No names or email address have < beyond the wrapper around email addresses
    3. Your list always has the entries separated with a comma then a space
    4. Your names are always separated from your email addresses by a space.