CODER
asked on
Extracting a particular string from a file in PERL (NEED ASAP)!!
Hello I had a question about getting values in PERL.
SAy i have a file with the following info
Database,pp.one.com,dbname 1,root,zxc 213cv
Database,pp.two.com,dbname 2,root,zxc 213cv
Database,pp.four.com,dbnam e3,root,zx c213cv
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
SAy i have a file with the following info
Database,pp.one.com,dbname
Database,pp.two.com,dbname
Database,pp.four.com,dbnam
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@string = map {(split/,/)[2]} <FILE>;
ASKER
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..
ASKER
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...
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...
for( @string ){
open IN,"<$_" or die "can't open $_ for input because $!";
open OUT,">$_" or die "can't open $_ for output because $!";
}
open IN,"<$_" or die "can't open $_ for input because $!";
open OUT,">$_" or die "can't open $_ for output because $!";
}
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) ;
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) ;
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) ;
$xx = 0;
open(FILE , "filename");
while ($line = <FILE>)
{
chomp($line);
$xx++;
next if ($xx < $n);
@all = split(/,/ , $line);
$string = $all[2];
last;
}
close(FILE) ;
ASKER
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
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
ASKER
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
--------------------------
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
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
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
ASKER
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
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
ASKER
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...
----------------------
##########################
#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...