freelanceprogrammers.org Forum Index » Perl

Parsing problem


View user's profile Post To page top
perl_1978 Posted: Mon Aug 23, 2004 6:23 pm


Joined: 07 May 2004

Posts: 7
Parsing problem
The following code is parsing out the /etc/shadow file and should
print the second column but for some reason it`s printing the third.

#!/usr/local/bin/perl -w

$SHADOW="/etc/shadow";
open (SH,$SHADOW) || die "Cannot open file";

while (<SH>) {

chomp;
(my $password) = (split /:/) [2];
print "$password
";
}
close(SH);

Any suggestions?
Reply with quote
Send private message
View user's profile Post To page top
merlynstoneh... Posted: Mon Aug 23, 2004 7:50 pm


Joined: 21 May 2004

Posts: 44
Parsing problem
>>>>> "perl" == perl 1978 <no_reply@yahoogroups.com> writes:

perl> The following code is parsing out the /etc/shadow file and should
perl> print the second column but for some reason it`s printing the third.

perl> (my $password) = (split /:/) [2];
perl> print "$password
";

Precisely.

0 1 2

2 is the third column.


--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Reply with quote
Send private message
View user's profile Post To page top
kchima01 Posted: Mon Aug 23, 2004 8:27 pm


Joined: 23 Aug 2004

Posts: 1
Parsing problem
Greetings,

The split function creates an array, which begins at zero [0];
therefore, you are accessing the 3rd element of the array. Try this
instead:

(my $password) = (split /:/) [1];

--- In perl_official@yahoogroups.com, perl_1978 <no_reply@y...> wrote:
> The following code is parsing out the /etc/shadow file and should
> print the second column but for some reason it`s printing the third.
>
> (my $password) = (split /:/) [2];
Reply with quote
Send private message
View user's profile Post To page top
perl_1978 Posted: Mon Aug 23, 2004 11:12 pm


Joined: 07 May 2004

Posts: 7
Parsing problem
Thanks merlynstonehengecom and kchima01 it`s fixed now.


--- In perl_official@yahoogroups.com, kchima01 <no_reply@y...> wrote:
> Greetings,
>
> The split function creates an array, which begins at zero [0];
> therefore, you are accessing the 3rd element of the array. Try
this
> instead:
>
> (my $password) = (split /:/) [1];
>
> --- In perl_official@yahoogroups.com, perl_1978 <no_reply@y...>
wrote:
> > The following code is parsing out the /etc/shadow file and should
> > print the second column but for some reason it`s printing the
third.
> >
> > (my $password) = (split /:/) [2];
Reply with quote
Send private message
View user's profile Post To page top
harish85_bt Posted: Thu Sep 02, 2004 1:18 am


Joined: 02 Sep 2004

Posts: 4
Parsing problem
hi people
i have a small problem....
i would like to know how to create 2 dimensional
arrays in perl...i`m not able to find this in any of
the perl books.....
Any good links or tutorials regarding this are
welcomed...
thanks in advance....
Rgds
Harish.

=====
HARISH.S





__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
Reply with quote
Send private message
View user's profile Post To page top
merlynstoneh... Posted: Thu Sep 02, 2004 5:50 pm


Joined: 21 May 2004

Posts: 44
Parsing problem
>>>>> "harish" == harish sundararaj <harish85_bt@...> writes:

harish> i have a small problem....
harish> i would like to know how to create 2 dimensional
harish> arrays in perl...

Perl doesn`t have formal two-dimensional arrays. It has arrays that
can contain arrayrefs, but all dereferencing is explicit, and so you
must maintain awareness of this fact while programming.

harish> i`m not able to find this in any of
harish> the perl books.....

You haven`t read either "Learning Perl Objects References and Modules"
(the "volume 2" of the "Learning Perl" series) or "Programming Perl"
then.

harish> Any good links or tutorials regarding this are
harish> welcomed...

"perldoc perlreftut" might be a good start. Or google my 200+ online
columns with

site:stonehenge.com references

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help