Solved

Extracting a particular string from a file in PERL  (NEED ASAP)!!

Posted on 2000-04-27
12
156 Views
Last Modified: 2010-03-05
Hello I had a question about getting values in PERL.
                      SAy i have a file with the following info

                      Database,pp.one.com,dbname1,root,zxc213cv
                      Database,pp.two.com,dbname2,root,zxc213cv
                      Database,pp.four.com,dbname3,root,zxc213cv

                      HOW DO I EXTRACT THE FOLLOWING
                      dbname1 .....dbname3
                       until there are no more to dbnames to grab.

                      How do I do this. Assuming the above info are all store in a                        file.
   
                      How do I get the string I desire(dbnmame1) assuming
                      the is only one line in the file.

                      Thanks alot.

                      PLEASE I NEED THIS LIKE NOW!!!!! I WILL REALLY APPRECIATE IT.

                      CODER '2000
0
Comment
Question by:CODER
  • 6
  • 3
  • 3
12 Comments
 
LVL 6

Accepted Solution

by:
christopher sagayam earned 75 total points
ID: 2756161
$xx = 0;
open(FILE , "filename");
while ($line = <FILE>)
{
chomp($line);
@all = split(/,/ , $line);
$string[$xx] = $all[2];
$xx++
}
close(FILE);

Now the @string will conmtain the required values .. namely dbname1 , dbname2 etc
0
 
LVL 84

Expert Comment

by:ozo
ID: 2757284
@string = map {(split/,/)[2]} <FILE>;
0
 

Author Comment

by:CODER
ID: 2757411
Chris18, thanks u are the one tha has helped me... can u send me your e-mail address so i can communicate with u thru e-mail if I need help.. thanks..
0
 

Author Comment

by:CODER
ID: 2757422
From: chris18
                                                            Date: Thursday, April 27 2000 - 10:19AM PDT

                      $xx = 0;
                      open(FILE , "filename");
                      while ($line = <FILE>)
                      {
                      chomp($line);
                      @all = split(/,/ , $line);
                      $string[$xx] = $all[2];
                      $xx++
                      }
                      close(FILE);

                      Now the @string will conmtain the required values .. namely dbname1 , dbname2 etc

NOW CHRIS IF I WANT TO OPEN ALL THE FILENAMES IN @STRINGS BECAUSE THEY WILL BE FILE NAMES, HOW CAN I OPEN THEM FOR INPUT OR OUTPUT.
THANKS...
AND IF THERE ARE 10 LINES AND I WANT TO GRAM ONLY THE DBNAME ON THE 4TH LINE HOW WOULD I ACCOMPLISH THIS??????
THANKS ALOT...
0
 
LVL 84

Expert Comment

by:ozo
ID: 2757613
for( @string ){
  open IN,"<$_" or die "can't open $_ for input because $!";
  open OUT,">$_" or die "can't open $_ for output because $!";
}
0
 
LVL 6

Expert Comment

by:christopher sagayam
ID: 2757878
for opening the files ..

you can ofcourse use

open(FILE , "$string[$xx]")

close(FILE)


for getting the Nth record in the file

use a different code ..


$n = 4 ;# say you want the 5th record in the file
                      $xx = 0;
                      open(FILE , "filename");
                      while ($line = <FILE>)
                      {
                      chomp($line);
$xx++;
next if ($xx < $n);
                      @all = split(/,/ , $line);

$string = $all[2];
                      }
                      close(FILE) ;

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:christopher sagayam
ID: 2757890
make a small change in the last code ....


                      $xx = 0;
                      open(FILE , "filename");
                      while ($line = <FILE>)
                      {
                      chomp($line);
$xx++;
next if ($xx < $n);
                      @all = split(/,/ , $line);

$string = $all[2];
last;
                      }
                      close(FILE) ;



0
 

Author Comment

by:CODER
ID: 2759859
if i try to open the files in @strings using this method
for( @string ){
                        open IN,"<$_" or die "can't open $_ for input because $!";
                        open OUT,">$_" or die "can't open $_ for output because $!";
                      }

here is the error/ warning   i get
Can't locate object method "OPEN" via package "IN" at check.pl line 22.

NOW IF I USE YOUR METHOD CHRIS
open(FILE , "$string[$xx]")

                      close(FILE)

DOES THIS OPEN ALL THE  DBNAMES or just the first one if there are more than one?

thanks again
0
 

Author Comment

by:CODER
ID: 2760154
here is another quick quest. guyz
------------------------------------------
Hello I need to open a database in a config file and then extract the dbname from the file and I already have that done. Now I want to know how to create a temporary table after opening the database.
Thanks alot.
HERE IS MY CODE AS IT LOOKS NOW
***********************************************************
#######################################################################
#Opening the Config file for input and parsing the file inorder
#to extract the database file(s)
#######################################################################

$counter = 0;
open(DBFILE, "RC_config");
while ($line = <DBFILE>)
{
  chomp($line);
  @all = split(/,/, $line);
  $dbfstring[$counter] = $all[2];
  $counter++;
}
close(DBFILE);
print @dbfstring;  #debug

#######################################################################
#Opening all the database(S) specified in the config file
#######################################################################

for (@dbfstring[$counter])
{
  #OPEN IN, "<$_" or die "can't open $_ for input because $!";
  #open %DATABASE onto the databases in config file
  dbmopen(%DATABASES, "$dbfstring", 0666);  
}


qUESTion???:how do i create a temp table.

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 2760232
What do you want the table to contain?

perl -Mdiagnostics -w
Scalar value @dbfstring[$counter] better written as $dbfstring[$counter] at -
        line 19 (#1)
   
    (W) You've used an array slice (indicated by @) to select a single element of
    an array.  Generally it's better to ask for a scalar value (indicated by $).
    The difference is that $foo[&bar] always behaves like a scalar, both when
    assigning to it and when evaluating its argument, while @foo[&bar] behaves
    like a list when you assign to it, and provides a list context to its
    subscript, which can do weird things if you're expecting only one subscript.
   
    On the other hand, if you were actually hoping to treat the array
    element as a list, you need to look into how references work, because
    Perl will not magically convert between scalars and lists for you.  See
    perlref.
 

Also, $counter will be > $#dbfstring, so @dbfstring[$counter] will be undefined
and even if it was defined, you've never defined $dbfstring
0
 

Author Comment

by:CODER
ID: 2760284
Well for now, due to the flow chart i have, i need to create atemp table  in all the opened databases which will later contain some informations.

Can u pls show me how to create the temp table(Actuall how to create a table)

do i need to use an sql command or some dbi stuff?
thanks...

I really need this like today....
Its fun cause i am really learning...... Thanks
get back o me soon
0
 

Author Comment

by:CODER
ID: 2760304
so is this fine now
----------------------
#######################################################################
#Opening all the database(S) specified in the config file
#######################################################################

for ($dbfstring[$counter])
{
  #OPEN IN, "<$_" or die "can't open $_ for input because $!";
  #open %DATABASE onto the databases in config file
  dbmopen(%DATABASES, "$dbfstring", 0666);  
}

will this open the database...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now