?
Solved

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

Posted on 2000-04-27
12
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 6

Accepted Solution

by:
christopher sagayam earned 225 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

762 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