Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^5: Optional Arguments..?

by poj (Prior)
on Jun 03, 2012 at 15:10 UTC ( #974152=note: print w/replies, xml ) Need Help??

in reply to Re^4: Optional Arguments..?
in thread Optional Arguments..?

You can't store all the data you need like this ;
# declare a hash of array to prepare storage of all # variables to be encoded together my $HoA{$sitename} = ["$loginid", "$password", "$url"]; open(FILEHANDLE, ">>passmgr.dat") or die("The file cannot be opened!"); my $encode = ST2614::encode($HoA, $ARGV[1]); print FILEHANDLE "$encode"; close FILEHANDLE;
Consider using a tab delimiter record format like this ;
sitename1 loginid1 encoded_password1 url1 sitename2 loginid2 encoded_password2 url2 sitename3 loginid3 encoded_password3 url3
Then you can build a HoA from the file like this
my $key = $ARGV[1] || 'secretkey'; my %HoA=(); open IN, '<','passmgr.dat' or die ("The file cannot be opened!"); while (<IN>){ chomp; my ($sitename,$id,$enc_password,$url) = split "\t",$_; my $password = ST2614::decode($enc_password, $key); $HoA{$sitename} = [$id,$password,$url]; # print "$sitename $id $password $url\n"; }
and save it to the file like this
open OUT, '>','passmgr.dat' or die ("The file cannot be opened!"); for my $sitename (sort keys %HoA){ my $id = $HoA{$sitename}[0]; my $enc_password = ST2614::encode($HoA{$sitename}[1], $key); my $url = $HoA{$sitename}[2]; print OUT join "\t",$sitename,$id,$enc_password,$url,"\n"; }

Replies are listed 'Best First'.
Re^6: Optional Arguments..?
by Watergun (Novice) on Jun 04, 2012 at 00:07 UTC

    Thanks for the reply!

    But could you kindly explain what the code you provided does, and how does it work? As I'm very new to Perl, I don't understand some of the code yet.

    If I am not wrong, you are only encoding the password, but my aim is to encode everything that the user entered.
      I have added some comments and changed it to encode/decode complete records i.e. all the data.
      #!perl use strict; use ST2614; my $key = $ARGV[1] || 'secretkey'; my %HoA=(); # open file to read open IN, '<','passmgr.dat' or die ("The file cannot be opened!"); # read records line at a time while (<IN>){ chomp; # remove carriage return/line feed # decode record my $record = ST2614::decode($_, $key); # split records into fields on tab my ($sitename,$id,$password,$url) = split "\t",$record; # build hash of arrays $HoA{$sitename} = [$id,$password,$url]; # print "$sitename $id $password $url\n"; } # open file to write open OUT, '>','passmgr.dat' or die ("The file cannot be opened!"); # loop through HOA using key to extract values from array for my $sitename (sort keys %HoA){ my $id = $HoA{$sitename}[0]; my $password = $HoA{$sitename}[1]; my $url = $HoA{$sitename}[2]; # build a record of fields seperated with tab my $record = join "\t",$sitename,$id,$password,$url; # store as encoded value print OUT ST2614::encode($record,$key)."\n"; }
      hope this helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://974152]
[Corion]: Cosmic37: Basically, you read one file into a hash, keyed by your key, and then match the lines from the second file to that hash
[Cosmic37]: note that the two files only have datetimes which may match whereas other data per line is different format in file1 and file2 - is that really intersection?
[jedikaiti]: Hi Monks
[Corion]: Cosmic37: Well, if you want to use only parts of a line for the key, see split or whatever other mechanism to extract the key from the line
[Corion]: Hi jedikaiti!
[Cosmic37]: is there a webpage for full CB for this chat rather than side panel chat?
[Cosmic37]: thank you for advice
[Cosmic37]: peach greets jedi
[Cosmic37]: ok this one works better fullpage chat
[Cosmic37]: test

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2017-06-29 16:24 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (672 votes). Check out past polls.