Re: A Hashing Question
by btrott (Parson) on Jun 17, 2000 at 01:13 UTC
|
You're overwriting the hash each line of the file. You're
opening files without checking the return code. You're
reading the file rather oddly.
You should be using a DBM file for this. This is what
DBM files were designed for. Take a look at perltie
and dbmopen. I don't know what DBM libraries you have
installed, but you'll definitely have at least SDBM_File,
because it comes with Perl. If you have DB_File, use that.
If you're insistent on storing this in a file, try this
code:
my $FILE = "joe.txt";
open FH, $FILE or die "Can't open $FILE: $!";
my %hash;
while (<FH>) {
chomp;
my($key, $val) = split /=>/;
$hash{$key} = $val;
}
close FH or die "Can't close $FILE: $!";
$myhash{$timestamp}=$newLine;
open FH, ">" . $FILE or die "Can't open $FILE: $!";
print FH join("\n", map $_ . "=>" . $hash{$_}, keys %hash), "\n";
close FH or die "Can't close $FILE: $!";
| [reply] [d/l] |
Why forced to do the job the hard way?
by merlyn (Sage) on Jun 17, 2000 at 02:02 UTC
|
Bigjoe lamented:
I am creating a script for a company. It is going to be run on an NT server with active Perl on it. I have no access to this server to add the Storage.pm onto it.
I see statements like this over and over again. Why do people accept jobs like this, or why do bosses require jobs to be done like this?
If I were a doctor, I'd not be told "try to save this patient, using only that little medical kit over there, and I hope there's enough band-aids". If I were a car mechanic, I wouldn't be told "Please fix this 98 jeep, but you have to use only the parts from that wrecking yard right there."
Why do people put up with this sad situation?
-- Randal L. Schwartz, Perl hacker | [reply] |
|
|
It really makes you feel for them... Makes me immensely
thankful for my current situation, where there are none who
would fetter us like that.
Given enough experience in the workplace, I think no one
would suffer situations like this. (When you're good enough
to threaten to leave -- or good enough to choose your dream
job, life would be much, much better, and suffering of
fools much, much less frequent.)
Russ
| [reply] |
|
|
Given the high demand for Perl programmers, I don't see any reason why _anybody_ needs to work under poor conditions. Just tell them, "Look, this is how it's done, there are reasons the programmers need it this way, and otherwise I won't work for you." And if they don't like it, well... there ARE lots of jobs for Perl programmers right now! You don't even need experience... if you're any good, you just write some cool programs as demos, and start taking short-term contracts.
Paris Sinclair | 4a75737420416e6f74686572
pariss@efn.org | 205065726c204861636b6572
I wear my Geek Code on my finger.
| [reply] |
|
|
I agree that if you don't stand up your rights, whatever rights they are, then you will not get the tools you need.
What I don't understand, is how a person can be in a position where they can upload a script, but yet can't install modules in the same dir as the script?
But, why would you want to run perl on NT anyway?
The Book of Larry says:
Perl is, in intent, a cleaned up and summarized version of that wonderful semi-natural language known as "Unix".
-- Larry Wall in <1994Apr6.184419.3687@netlabs.com>
Paris Sinclair | 4a75737420416e6f74686572
pariss@efn.org | 205065726c204861636b6572
I wear my Geek Code on my finger.
| [reply] |
|
|
In my case, our IS department doesn't trust us accessing the Web server. We have to have a working model of our scripts on sim server first. Then they test it and move the script to their server.
I would love a job where I could program PERL on a Linux system. But I don't have the experience to get hired for it yet. So when the company that I work for asked for this I offered to get experience for my resume to get one of those great jobs.
--BigJoe
| [reply] |
|
|
|
|
|
|
|
|
Re: A Hashing Question
by cwest (Friar) on Jun 17, 2000 at 01:58 UTC
|
Just to give an example of lhowards idea...
use Data::Dumper;
my %hash1 = (
one => 1,
two => 2,
thr => 3,
);
# You would write Dumper(\%hash1) to a file...
# then read into scalar e.g. $var .= $_ while <>;
my $var = Dumper \%hash1;
my $VAR1; # This shut's up -w/strict ;-)
my %hash2 = %{eval $var};
# %hash2 now equals %hash1!
--
Casey
| [reply] |
Re: A Hashing Question
by lhoward (Vicar) on Jun 17, 2000 at 01:41 UTC
|
How about using Data::Dumper?
It comes with Perl so you don't have to get them
to install it and can
provide similar functionality to Storable.
| [reply] |
RE: A Hashing Question
by Anonymous Monk on Jun 17, 2000 at 16:42 UTC
|
Is the problem that the hash is in a random order? It says on page 8 of the Camel book that: "if you use a hash in list context, it 'll convert the hash back to a list of key/value pairs, in a weird order. However, page 182 points out that you can order the output by value simply by saying: foreach $key (sort {$myhash{b} <=>$myhash{a} } keys %myhash) {printf "%4d %s\n", $myhash{$key},$key;}
| [reply] |