split .ini file for passwords against usernames

I

ipog_1234

Hi:
Purpose: to read .ini file to get a password for a respective user.

Problem:I was running this perl script and could not figure out how to
search for the username(here joe) and print the password.

..ini File: contains username and password with tab sequence in-between.

Code:
#!/software/bin/perl -w
use warnings;
$file='/home/work/password.ini';
$username="joe";
open(INFO, $file);
while(<INFO>){
if ( $_ ~= /^$username/ ){
($password)=split[1];

}
}
print "$password,\n";
close(INFO);
 
J

Josef Moellers

Hi:
Purpose: to read .ini file to get a password for a respective user.

Problem:I was running this perl script and could not figure out how to
search for the username(here joe) and print the password.

.ini File: contains username and password with tab sequence in-between.

Code:
#!/software/bin/perl -w
use warnings;
$file='/home/work/password.ini';
$username="joe";
open(INFO, $file);
while(<INFO>){
if ( $_ ~= /^$username/ ){
($password)=split[1];

}
}
print "$password,\n";
close(INFO);

1. Your program doesn't even compile!
2. How does your program fail? What is the output you see?

Try changing "($password)=split[1];" into "(undef,$password) = split;"
or "$password = (split)[1];"
I'm not entirely sure (make that: "I don't have a clue") what split[1]
does. It seems to be valid, but it doesn't seem to split on white space.
 
A

Anno Siegel

Hi:
Purpose: to read .ini file to get a password for a respective user.

Problem:I was running this perl script and could not figure out how to
search for the username(here joe) and print the password.

.ini File: contains username and password with tab sequence in-between.

Code:
#!/software/bin/perl -w
use warnings;

"use strict" is missing.
$file='/home/work/password.ini';
$username="joe";
open(INFO, $file);
while(<INFO>){
if ( $_ ~= /^$username/ ){
($password)=split[1];

A list slice needs an extra pair of parentheses. The parentheses around
$password are not needed:

my $password = (split)[1];
}
}
print "$password,\n";
close(INFO);

Anno
 
P

Paul Lalli

Josef said:
I'm not entirely sure (make that: "I don't have a clue") what split[1]
does.

$ perl -MO=Deparse -e'($password) = split[1]'
$password = split(/[1]/, $_, 2);
-e syntax OK
It seems to be valid, but it doesn't seem to split on white space.

It apparently assumes '[1]' is the pattern on which to split. Now,
where the limit of 2 came from, I have no idea.

Paul Lalli
 
P

Paul Lalli

Paul said:
$ perl -MO=Deparse -e'($password) = split[1]'
$password = split(/[1]/, $_, 2);
-e syntax OK

It apparently assumes '[1]' is the pattern on which to split. Now,
where the limit of 2 came from, I have no idea.

Oh. Yes I do. Because I provided a list context to split, and that
list contained only one item, Perl was smart enough to know that there
was no need to split more than once, as everything after the first
token would be thrown away. So it implicitly gave a limit of 2, to
save needless work.

Perl is often smarter than even I give it credit for...

Paul Lalli
 
J

Josef Moellers

Paul said:
Paul said:
$ perl -MO=Deparse -e'($password) = split[1]'
$password = split(/[1]/, $_, 2);
-e syntax OK

It apparently assumes '[1]' is the pattern on which to split. Now,
where the limit of 2 came from, I have no idea.


Oh. Yes I do. Because I provided a list context to split, and that
list contained only one item, Perl was smart enough to know that there
was no need to split more than once, as everything after the first
token would be thrown away. So it implicitly gave a limit of 2, to
save needless work.

Thanks. I hope I remember the "-MO=Deparse" switch in the future.
Perl is often smarter than even I give it credit for...

Doesn't this apply to people as well (sometimes) ;-)?

I, at least, am now a little bit smarter than before.

Cheers,

Josef
 
I

ipog_1234

Hi all:
I am stuck at 2 compilation errors, before trying out the bits of code
suggested in this thread.

syntax error at testread5.pm line 8, near "$_ ~"
syntax error at testread5.pm line 15, near "}"

Do you the Regex to match $username needes some tweaking.

thankyou.
ipog.

Josef said:
Paul said:
Paul said:
$ perl -MO=Deparse -e'($password) = split[1]'
$password = split(/[1]/, $_, 2);
-e syntax OK

It apparently assumes '[1]' is the pattern on which to split. Now,
where the limit of 2 came from, I have no idea.


Oh. Yes I do. Because I provided a list context to split, and that
list contained only one item, Perl was smart enough to know that there
was no need to split more than once, as everything after the first
token would be thrown away. So it implicitly gave a limit of 2, to
save needless work.

Thanks. I hope I remember the "-MO=Deparse" switch in the future.
Perl is often smarter than even I give it credit for...

Doesn't this apply to people as well (sometimes) ;-)?

I, at least, am now a little bit smarter than before.

Cheers,

Josef
 
P

Paul Lalli

In future replies, please reply to the actual message you're replying
to, not to some arbitrary post in the same thread.

In future replies, please place your new text *below* the text to which
you are replying.

In future replies, please trim the text to which you are replying to a
relevant amount of quoting material.

(Have you read the posting guidelines for this group yet?)

Hi all:
I am stuck at 2 compilation errors, before trying out the bits of code
suggested in this thread.

Why would it be 'before'? You know the code you're trying to compile
is wrong, but you're just going to ignore it until the interpreter
stops giving you errors?
syntax error at testread5.pm line 8, near "$_ ~"
syntax error at testread5.pm line 15, near "}"

Do you the Regex to match $username needes some tweaking.

No, I think the two operators ~ and = in your code need to be replaced
by the single operator =~

~= is not an operator. It is the two operators ~ and =, which happen
to be sitting next to each other.

Paul Lalli
 
I

ipog_1234

if ( $_ =~ /^$username/ ){
(undef,$password) = split;
}
the above regular expression "~=" was transposed to "=~" and the line
"(undef,$password) = split;" worked out well.
however, i modified it to put while stat into a single line:
#!/software/bin/perl -w
use warnings;
#use strict;
$file='/home/work/password.ini';
open(INFO, $file) or die "could not open '$file' $!";
$username="joe";
while(<INFO>){if ( $_ =~ /^$username/ ){(undef,$password) = split;}}
print "$password \n";
close(INFO);

My Question: can i put the entire open,while and close in a single line
of code.
 
G

Glenn Jackman

At 2005-10-12 01:20PM said:
}
the above regular expression "~=" was transposed to "=~" and the line
"(undef,$password) = split;" worked out well.
however, i modified it to put while stat into a single line:
#!/software/bin/perl -w
use warnings;
#use strict;
$file='/home/work/password.ini';
open(INFO, $file) or die "could not open '$file' $!";
$username="joe";
while(<INFO>){if ( $_ =~ /^$username/ ){(undef,$password) = split;}}
print "$password \n";
close(INFO);

My Question: can i put the entire open,while and close in a single line
of code.


perl -lane 'if (/^joe/) {print $F[1]; last}' /home/work/password.ini
 
I

ipog_1234

I tried this one liner:
my $pass;
$pass=`perl -lane 'if (/^joe/) {print $F[1];last}'
/home/work/password.ini`

and got this compilation error:
Name "main::F" used only once: possible typo at greptest2.pm line 7.
Use of uninitialized value in concatenation (.) or string at
greptest2.pm line 7.

$F should be splitting the column and print second column!!Does perl
recogize this.??
 
J

Josef Moellers

}
the above regular expression "~=" was transposed to "=~" and the line
"(undef,$password) = split;" worked out well.
however, i modified it to put while stat into a single line:
#!/software/bin/perl -w
use warnings;
#use strict;
$file='/home/work/password.ini';
open(INFO, $file) or die "could not open '$file' $!";
$username="joe";
while(<INFO>){if ( $_ =~ /^$username/ ){(undef,$password) = split;}}
print "$password \n";
close(INFO);

My Question: can i put the entire open,while and close in a single line
of code.

You can put everything, from the first "use warnings;" to the very last
";" onto a single line. There are only a few places where perl actually
cares about the presence or absence of newlines. You could even squeeze
our a few more blanks to save space.
Not that it makes the program any more readable ...

Why would you want to do that? Do newline characters cost extra at your
site? Can I set up a business there to sell newline characters (I could
send you a megabyte of them at a very competitive price ;-)

Your program code is already pretty dense and, as such, hard to read.
Note that spaces, comment lines, and even some empty lines make your
code much better to read and better to understand and maintain in the
future.
 
E

Eric Bohlman

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
I tried this one liner:
my $pass;
$pass=`perl -lane 'if (/^joe/) {print $F[1];last}'
/home/work/password.ini`

and got this compilation error:
Name "main::F" used only once: possible typo at greptest2.pm line 7.
Use of uninitialized value in concatenation (.) or string at
greptest2.pm line 7.

$F should be splitting the column and print second column!!Does perl
recogize this.??

Your problem is that backticks provide a double-quotish context, so your
program is trying to interpolate $F[1] into the command string *before*
executing it. You need to either escape the dollar sign with a backslash,
or replace the backticks with qx'' (see "quotes and quote-like operators"
in perlop).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top