who can help me figure out some PERL script?
Guest
Posts: n/a
Originally Posted by 1995BvSSE
You can also redirect output from pretty much any program by using the ">" character, and then print the file. For example:
perl myscript.pl >output.txt
Hope that helps.
perl myscript.pl >output.txt
Hope that helps.
By the way, if it were me, I'd write the script "more like this"
But the beauty of Perl is that it is quite flexible. Ask 5 Perl programmers to write something, you'll get 5 totally different scripts.
But the beauty of Perl is that it is quite flexible. Ask 5 Perl programmers to write something, you'll get 5 totally different scripts.
Code:
use strict;
use warnings;
#always declare variables, required with use strict
my $in;
my %myhash;
my @keyarray;
my @valarray;
#Take input from the user, tokenize the input and store it into a hash
#We make the loop "infinite" until the "last" condition is met below
while(true) {
print "Enter a key/value pair separated by a colon\n(quit to finish)\t";
$in = <STDIN>;
chomp $in;
#using a "last" statement here precludes the need to have the "initial" set of
#duplicate logic in the beginning of the script. i.e., we'll leave the loop
#once our exit criteria is met
last if $in eq "quit";
(my @result) = split(/:/, $in);
#Always verify your input!
#Note that in Perl, treating an array as a scalar will give you the size.
if (@result != 2)
{
print "ERROR: input not understood\n";
}
else
{
$myhash{$result[0]} = $result[1];
}
}
#iterate through each element in the hash in sorted fashion
foreach my $temp (sort keys %myhash) {
#store the hash keys in keyarray
push(@keyarray,$temp);
#store the hash values in valarray
push(@valarray,$myhash{$temp});
}
#Take elements off the end of the array, one at a time, thus reversed sorted order
while(@keyarray > 0) {
print("Key: ".pop(@keyarray)." Value: ".pop(@valarray)."\n");
}
exit(0);
Thread
Thread Starter
Forum
Replies
Last Post
DADillac
Audio (and aftermarket electronics)
8
Sep 25, 2008 05:28 PM
bonnie94ssei
Everything Electrical & Electronic
9
Feb 20, 2004 04:48 PM




