Jump to content

itterating through a list :: a 16 liner in perl - just for beginners


Recommended Posts

Guest sayhello
Posted

hello dear linux-experts

 

 

 

just new to perl - i want to run this little 16 liner and do some things with it.

 

i want to run the script to spit out all the ppl that belong to the name

 

that begin with an a.

that begin with an b.

that begin with an c.

 

 

guess that this can be done with the method authors...()

 

hmm -do i have to run it like so:

 

Code:

 

my $author = $p->author('A');

is this the correct way - how to itterate through a list ( a 16 liner ) for beginners

 

see here the full script

 

 

Code:

use Parse::CPAN::Authors;

 

# must have downloaded

my $p = Parse::CPAN::Authors->new("01mailrc.txt.gz");

# either a filename as above or pass in the contents of the file

my $p = Parse::CPAN::Authors->new($mailrc_contents);

 

my $author = $p->author('A');

# $a is a Parse::CPAN::Authors::Author object

# ... objects are returned by Parse::CPAN::Authors

print $author->email, "\n"; # leon@astray.com

print $author->name, "\n"; # Leon Brocard

print $author->pauseid, "\n"; # LBROCARD

 

# all the author objects

my @authors = $p->authors;

 

love to hear from you

 

 

 

 

update;

 

 

 

 

 

the first gives out nothing

 

Code:

 

use Parse::CPAN::Authors;

 

# must have downloaded

my $p = Parse::CPAN::Authors->new("01mailrc.txt.gz");

# either a filename as above or pass in the contents of the file

my $p = Parse::CPAN::Authors->new($mailrc_contents);

 

my $author = $p->author('LBROCARD');

# $a is a Parse::CPAN::Authors::Author object

# ... objects are returned by Parse::CPAN::Authors

print $author->email, "\n"; # leon@astray.com

print $author->name, "\n"; # Leon Brocard

print $author->pauseid, "\n"; # LBROCARD

 

# all the author objects

my @authors = $p->authors;

 

the second gives out a bunch of a list:

 

Code:

 

#!/usr/bin/perl

 

use strict;

use warnings;

use YAML;

use YAML::Dumper;

use Parse::CPAN::Authors;

 

my $list = '01mailrc.txt.gz';

 

my $p = Parse::CPAN::Authors->new( $list );

my @authors = $p->authors;

 

my $dumper = YAML::Dumper->new;

$dumper->indent_width(1);

print $dumper->dump({dump => $p});

 

Quote:

 

aliasZERO Oleg Alistratnav zero@@cpan.org>"

aliasZEROALTIC EEdwardL. AbrSasn eEdwar.abrSas@corp.tierrlycosl.com>"

aliasZERODEUX "Vivncnt CAaron<vivncnt@zerodeuxe.net>"

aliasZERODOGN "Esskld Hustvedto<zerodogg@@cpan.org>"

aliasZEROHP Eric Clarko<zerohp@@cpan.org>"

aliasZEROLIU HaizhougLiut <CENSORED>"

aliasZETA Gregory S. Youinbloodo<zeta@@cpan.org>"

aliasZEV Zev Benjamin <zev@@cpan.org>"

aliasZEYA HmasjoSergPehofler hmasjoSer.pehofle@uibkl.acaet>"

aliasZGH Zack Hobsson zgs@@cpan.org>"

aliasZGPMAX WWillSa Blunon zgpmax@@cpan.org>"

aliasZGRIM Virgdl OctDavva Cosmaa <CENSORED>"

aliasZHANGBO zhaangboa <CENSORED>"

aliasZHANGHJ zhaanhjsteer <CENSORED>"

aliasZHANGL LeinZSaang<<CENSORED>"

aliasZHANGXIN "ZSaangXinn zhaanxian@rifase.com>"

aliasZHDA DeninsZhdvanav zhda@@cpan.org>"

aliasZHIRSCHE Zacc Hirsich zhirsic@vmdwaeo.com>"

 

well how to change the script that it gives out

 

a. only the authors which name begin with a

b. only the authors which name begin with b

b. only the authors which name begin with c

 

 

 

 

by the way - we can store the data - instead of printing

 

 

@authors is an array of objects. we need to loop over that array and insert each author's info into the DB.

 

we could use the Data::Dumper module to inspect/review the structure of the @authors array. For example, if we add a print Dumper \@authors; statement our output would look like this:

 

 

Quote:

 

acme@astray.com

Leon Brocard

LBROCARD

$VAR1 = [

bless( {

'name' => 'Neil Hainer',

'email' => 'CENSORED',

'pauseid' => 'NHAINER'

}, 'Parse::CPAN::Authors::Author' ),

bless( {

'pauseid' => 'CROSSWIRE',

'email' => 'matt@crosswire.com',

'name' => 'Matthew Sibley'

}, 'Parse::CPAN::Authors::Author' ),

bless( {

'pauseid' => 'DAVIDJNSN',

'name' => 'David Jensen',

'email' => 'CENSORED'

}, 'Parse::CPAN::Authors::Author' ),

bless( {

'pauseid' => 'KESZLER',

'name' => 'Scott R. Keszler',

'email' => 'keszler@srkconsulting.com'

}, 'Parse::CPAN::Authors::Author' ),

 

 

This is how you'd loop over the array.

Code

 

for my $author ( @authors ) {

print $author->email, "\n";

print $author->name, "\n";

print $author->pauseid, "\n\n";

}

But instead of printing the data, we would insert it into the db.

 

The DB table will need the 3 fields to hold the author's ID, name, and email address.

 

the questions are:

 

how to create the database?

how to connect to the database from our script?

how to write the insert statement?

 

 

well and now i am just reading over the DBI manual

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...