Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Array issue

by Anonymous Monk
on Jan 28, 2004 at 20:11 UTC ( #324765=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks , I am new to perl and this site : my question is , I have the following file which I am reading :
[PASSWORDS] GUY ddd GIRL dfd33 BOY df341
I am reading it as follow
my @listOfPass; open(INPUT , "<$input_file" ) or die "couldn't open"; my ($section,%passwds); while(<CONFILE>) { chomp; if (/^\[(.*?)\]$/) { $section = $1; next; } if ($section eq 'PASSWORDS') { my ($mkt,$pass) = split; $passwds{$mkt} = $pass; $passwds{$pass} = $mkt; @listOfPass = "$pass"; # <------ not working :(( }#end while loop close (INPUT);
What I am trying tod do is after getting the passwords , store it into the array that is declared outside the open statment, when I try to store it into the array , it says it is not declare and when I declare it inside , it can't see it outside that loop. I just want to have all the passwords stored in a global array in the order it was read from the config file so I can pass that array to a different subs inside my program. I hope I am clear ,,, thanks

Replies are listed 'Best First'.
Re: Array issue
by Ovid (Cardinal) on Jan 28, 2004 at 20:24 UTC

    I'd really need more of your code to understand what's happening, but cleaning up your indentation revealed that you were missing a curly brace. Also, you want to push the passwords onto the list. Assigning a password to the list would simply overwrite the last password.

    #!/usr/local/bin/perl use strict; use warnings; my @listOfPass; + open(INPUT , "<$input_file" ) or die "couldn't open ($input_file) :$!" +; + my ($section,%passwds); while(<CONFILE>) { chomp; if (/^\[(.*?)\]$/) { $section = $1; next; } if ($section eq 'PASSWORDS') { my ($mkt,$pass) = split; $passwds{$mkt} = $pass; $passwds{$pass} = $mkt; push @listOfPass, $pass; } }#end while loop close (INPUT);

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Array issue
by Theo (Priest) on Jan 28, 2004 at 20:20 UTC
    In the code you posted, it looks like you didn't close the last IF statement with a curly brace. The WHILE is closed, but not the IF. (I guess really, the closing brace looks like the close for the IF which leaves the WHILE un-closed.)

    -Theo-
    (so many nodes and so little time ... )

Re: Array issue
by ysth (Canon) on Jan 28, 2004 at 20:22 UTC
    Use push @listOfPass, $pass to add each password to the end of the array or (less commonly used, except perhaps with @INC) unshift @listOfPass, $pass to add each to the beginning of the array.
Re: Array issue
by monktim (Friar) on Jan 28, 2004 at 20:32 UTC
    use strict; use warnings; my @listOfPass; my ($section,%passwds); while(<DATA>) { chomp; if (/^\[(.*?)\]$/) { $section = $1; next; } elsif ($section eq 'PASSWORDS') { my ($mkt,$pass) = split; push @listOfPass, $pass; } }#end while loop print join(',', @listOfPass); __DATA__ [PASSWORDS] GUY ddd GIRL dfd33 BOY df341
    UPDATE I just reread the OP. The passwords need to be in the order that they were received. I modified this code to use the push function like Ovid suggested. I got too trigger happy with my caffeine induced post :)
Re: Array issue
by Benedictus (Beadle) on Jan 28, 2004 at 20:27 UTC

    When you 'open' the input file, you are associating it with the INPUT filehandle, but you are reading from the CONFILE filehandle.

    Also, as others have mentioned, you appear to be missing a curly brace. You would do well to 'use strict' as well.

    Benedictus

Re: Array issue
by pelagic (Priest) on Jan 28, 2004 at 20:31 UTC
    Hi
    I'll try to explain what each of your stmts does:

     my ($mkt,$pass) = split;
    assign first word of line (user or whatever that is) to $mkt
    and Password (second word of line) to $pass

    $passwds{$mkt} = $pass;
    assign Password to hash/key %passwds/$mkt
    assign User to hash/key %passwds/$pass
    At the end you got a hash with every User _AND_ every Password as key. The values contain vice versa the Passwords and the Users.
    Now it's impossible to have just a list of the Passwords because you cannot distict them anymore.

    just do:
    $passwds{$mkt} = $pass; in your loop and then you can get all Users or all Passwords like that:
    @listOfPass = (values %passwds); @listOfUsers = (keys %passwds);
    Imre

    PS
    close if stmt with } before accessing the whole list ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://324765]
Approved by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-02-07 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?