LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2014, 05:37 AM   #1
sayhello_to_the_world
Member
 
Registered: May 2013
Posts: 221

Rep: Reputation: Disabled
how to itterate though a list of results - and store int to db


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});




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:umper 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:




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[/QUOTE]
 
Old 06-29-2014, 10:14 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180
Quote:
Originally Posted by sayhello_to_the_world View Post
hello dear linux-experts
just new to perl - i want to run this little 16 liner and do some things with it.
No, sorry...you've been posting perl questions for over a year now. You're not 'new' to it.
Quote:
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 -
Again, as with other threads you've posted about programming/scripting, if it works, it's the 'correct way'. But in this case, no, it isn't because you (again), didn't read the documentation or show effort of your own, as you've been asked to MANY times, and keep saying you will. If you did read the CPAN documentation, you'd see that the "author" function does what it implies...it returns ONE AUTHOR. If you want to return several, the "authorS" tag does that...which is clearly documented.
Quote:
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;
Sorry, that is NOT the 'full script'...it is a PART of a script that you (again), directly lifted from another website...in this case, CPAN. The full script is on this page:
https://metacpan.org/source/LBROCARD...les/authors.pl
...with the snippet that you posted here:
https://metacpan.org/pod/Parse::CPAN::Authors
Quote:
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});
The first is only a code snippet, and won't run at all, the way you posted it. The second is returning some data, but you don't actually bother POSTING what it returned. How do you think we can help with anything, when you don't post the relevant details???? Based on what you posted, it would seem to return ALL the authors, since that's exactly what you're asking it to do. If so, then put a conditional check in to look at the first character of the name returned, and if it matches, do something with it.
Quote:
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
You examine the output, and act accordingly, just like you would for any other variable.
Quote:
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:umper 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: 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?
However you want...since you've asked many MySQL questions before, as well as several about both Perl and PHP interacting with MySQL, you have THREE methods. Pick one.
Quote:
how to connect to the database from our script?
how to write the insert statement?
Go and look at any of your other threads where you asked about this, or any of the many, MANY thousands of examples you can find on Google.
Quote:
well and now i am just reading over the DBI manual
You should have already done that, since you said you were going to several times in the past, and you were even going to come back and post your solution:
http://www.linuxquestions.org/questi...db-4175506044/
http://www.linuxquestions.org/questi...db-4175502185/

...which still hasn't happened. AGAIN...you need to start showing some actual effort of your own, and show that you're learning something. Asking almost identical questions over and over (like how to connect to MySQL, create a database, etc), is pointless. If you can't apply anything that you've learned, then please just hire someone to do this work for you.

Last edited by TB0ne; 06-29-2014 at 10:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List of Humble Store games dugan Linux - Games 5 01-22-2013 10:32 AM
Store a list of records Huamin Programming 3 06-15-2012 12:32 AM
Store line from text file into list C++ lalhad21 Programming 2 02-06-2011 10:30 AM
How can I store (and/or) print a list of installed software for later use? camera3043 Linux - Software 10 02-09-2010 12:50 AM
How do list the results from my search form? Alexander.s Programming 4 05-09-2005 11:01 AM


All times are GMT -5. The time now is 01:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration