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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Arrays - Getting a single column from more than one row
by Fletch (Bishop) on Aug 23, 2006 at 17:11 UTC
Re: Arrays - Getting a single column from more than one row
by Joost (Canon) on Aug 23, 2006 at 17:19 UTC
Re: Arrays - Getting a single column from more than one row
by davido (Cardinal) on Aug 23, 2006 at 17:21 UTC

    Is @emails a list of lists, or is it a list of strings with some sort of delimiter between fields?

    Assuming it's a list of lists:

    foreach my $record ( @emails ) { my $email_address = $record->[1]; # now do something with your email address. }

    Or maybe it's a list of strings with space-delimited fields (that's problematic):

    foreach my $record ( @emails ) { if( $record =~ m/\S+\s+(\S+)/ ) { my $email_address = $1; # Do something with $email_address } else { die "Bad record format.\n"; }

    That assumes that the name field contains only a single name, and that there are no spaces in the address field. Maybe it's more like this:

    $record =~ m/(\S+\@\S+)$/

    ....which would ignore the first field, and simply look for the final field, which should contain no whitespace, and should include an '@'.

    Email addresses are pretty hard to match accurately, but if your data set is fairly clean this should do the trick.

    If you're using this to harvest email addresses for spamming, shame on you. But I'll give you the benefit of the doubt.


    Dave

Re: Arrays - Getting a single column from more than one row
by planetscape (Chancellor) on Aug 24, 2006 at 05:07 UTC

    If I am not mistaken, I believe you want a "slice" of your data structure.

    IMHO, one of the best discussions of complex data structures, and how to access their innards (using slices and so on), is to be found in Chapter 3. References and complex data structures of perlinter.pdf, available from Perl Training Australia. You'd probably want the most current version of the course notes, though, which have been reworked into progperl.pdf, available from the same page noted above.

    HTH,

    planetscape
Re: Arrays - Getting a single column from more than one row
by jdtoronto (Prior) on Aug 23, 2006 at 17:25 UTC
    And for the final suggestion, if this actually relates to getting something from a database, see the tutorials in the monastery.

    jdtoronto

Re: Arrays - Getting a single column from more than one row
by xorl (Deacon) on Aug 23, 2006 at 17:10 UTC
    Try something like this
    my @emails = ({"id" => "1", "name" => "Foo", "email" => "me\@foo.com"} +, {"id"=>"2", "name" => "Bar", "email" => "bar\@foo.com"} ); foreach (@emails) { print $_->{"email"}; }
    I would actually suggest putting all of info in a database and then using DBI to get the info out. It's much easier to manage that way and you can easily specify which field you want with just a few lines of code.
Re: Arrays - Getting a single column from more than one row
by GrandFather (Saint) on Aug 23, 2006 at 22:12 UTC

    Often providing a little example script and some sample data makes a huge difference to how others perceive your question.

    When you provide such a sample, where possible make it self contained with the fewest possible dependencies on non-core modules. Keep any data succinct, but make sure it encompases any important edge cases.


    DWIM is Perl's answer to Gödel