[30568] in Perl-Users-Digest
Perl-Users Digest, Issue: 1811 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 21 21:09:47 2008
Date: Thu, 21 Aug 2008 18:09:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 21 Aug 2008 Volume: 11 Number: 1811
Today's topics:
hang when I run perl -MCPAN -e 'install Net::SSH::Perl' <stephanecharette@gmail.com>
Re: Help: Filehandle problem <tadmc@seesig.invalid>
Help: Reading file and creating an associative array HerbF@earthlink.net
Re: Help: Reading file and creating an associative arra <jurgenex@hotmail.com>
Re: Help: Reading file and creating an associative arra <spamtrap@dot-app.org>
Re: Help: Reading file and creating an associative arra <spamtrap@dot-app.org>
Re: Help: Reading file and creating an associative arra (Jens Thoms Toerring)
Re: how to report which specific variable is uninitiali <jurgenex@hotmail.com>
Re: how to report which specific variable is uninitiali <rvtol+news@isolution.nl>
Re: how to report which specific variable is uninitiali xhoster@gmail.com
Re: how to report which specific variable is uninitiali <jurgenex@hotmail.com>
Re: how to report which specific variable is uninitiali <news1234@free.fr>
Re: how to report which specific variable is uninitiali <ben@morrow.me.uk>
Re: how to report which specific variable is uninitiali <tadmc@seesig.invalid>
Re: OT news server (fidokomik\)
Re: printing two consecutive lines <1usa@llenroc.ude.invalid>
Re: printing two consecutive lines <spamtrap@dot-app.org>
Re: Reading file and creating an associative array <rvtol+news@isolution.nl>
Re: Reading file and creating an associative array <edMbj@aes-intl.com>
Re: Reading file and creating an associative array <edMbj@aes-intl.com>
Re: set default value for undefined variables <tzz@lifelogs.com>
Re: set default value for undefined variables <jurgenex@hotmail.com>
Re: set default value for undefined variables <news1234@free.fr>
Re: The Importance of Terminology's Quality <martin@see.sig.for.address.invalid>
Re: The Importance of Terminology's Quality <arne@vajhoej.dk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 21 Aug 2008 15:59:34 -0700 (PDT)
From: Stephane Charette <stephanecharette@gmail.com>
Subject: hang when I run perl -MCPAN -e 'install Net::SSH::Perl'
Message-Id: <7675bc73-4336-4c0c-9461-76b9141756f8@n33g2000pri.googlegroups.com>
I'm trying to get Net::SSH::Perl installed. I did install
Math::BigInt::GMP, Math::GMP, Math::Pari, and various other modules
without problem.
But when I run "perl -MCPAN -e 'install Net::SSH::Perl'" the whole
thing hangs at this point:
-----------------
Manifying blib/man3/Net::SSH::Perl::Cipher::DES.3pm
Manifying blib/man3/Net::SSH::Perl::Packet.3pm
Manifying blib/man3/Net::SSH::Perl::Key::RSA.3pm
/usr/bin/make -- OK
Running make test
PERL_DL_NONLAZY=3D1 /usr/bin/perl "-MExtUtils::Command::MM" "-e"
"test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/01-
compile.....ok
t/02-
buffer......ok
t/03-packet......ok 1/10
-----------------
At this point it consumes none of the CPU, but never completes. CTRL
+C breaks out and aborts the installation. I see plenty of posts
about this when searching with Google, but no answer. Can anyone
help?
I'm using Ubuntu 8.04-32bit i686 arch, perl 5.8.8 that ships with
Ubuntu.
St=E9phane
------------------------------
Date: Thu, 21 Aug 2008 18:44:54 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Help: Filehandle problem
Message-Id: <slrngarvfm.rco.tadmc@tadmc30.sbcglobal.net>
Jürgen Exner <jurgenex@hotmail.com> wrote:
> Tad J McClellan <tadmc@seesig.invalid> wrote:
>
>> perldoc -f external
>
> Tad, you are really getting old :-)
Yeah.
Thanks for reminding me.
Maybe I can do the same for you some day.
> No documentation for perl function 'external' found
Ack! I used to copy/paste from the post to ...
>> How can I capture STDERR from an external command?
... get the question. But my terminal puts funny codes in it
so now I grep the indexes of the *.pod files that I made.
Looks like maybe I better start doing both.
> s/-f/-q/
Hey! None of that around here.
This is a family newsgroup.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 21 Aug 2008 12:50:23 -0700
From: HerbF@earthlink.net
Subject: Help: Reading file and creating an associative array
Message-Id: <ctgra45113ma24tljirotov59dh60safeh@4ax.com>
Semi-newbie here. Sorry for posting what is probably a simple question, but
I'm stumped.
I want to read in the records of a file and create an associative array with
the key to each array element being one of the fields of the record and the
value of each array element being a different field of the same record.
I'm trying this, but it's not working:
while (<TFILE>) {
@clientRecord = split /{/,$_;
$user = $clientRecord[0];
$clientName[$user] = $clientRecord[2];
}
Later in processing, I seek to use a defined $user to fetch
$clientName[$user]. I get a null value.
How do I read in the file and set up the associative array?
TIA,
--
HerbF
------------------------------
Date: Thu, 21 Aug 2008 20:19:52 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Help: Reading file and creating an associative array
Message-Id: <q0jra4h24c1845cb8oh7gpfsktf7o5qm5b@4ax.com>
HerbF@earthlink.net wrote:
>Semi-newbie here. Sorry for posting what is probably a simple question, but
>I'm stumped.
>
>I want to read in the records of a file and create an associative array with
>the key to each array element being one of the fields of the record and the
>value of each array element being a different field of the same record.
>
>I'm trying this, but it's not working:
>
>while (<TFILE>) {
> @clientRecord = split /{/,$_;
> $user = $clientRecord[0];
> $clientName[$user] = $clientRecord[2];
I believe you simply using the wrong type of braces.
Assuming $user is an actual text (not a number) then the statement above
is the same as
$clientName[0] = $clientRecord[2];
because the numerical value of a text is 0 and arrays are indexed by
numbers. Probably not what you had in mind.
You mentioned associative arrays (which are more commonly known as
hashes). To assign a hash value use curly braces.
$clientName$user} = $clientRecord[2];
>}
>
>Later in processing, I seek to use a defined $user to fetch
>$clientName[$user]. I get a null value.
Probably the same here: you are accessing elements in the array
@clientName while probably you want to access elements in the hash
%clientName.
BTW: had you used 'use warning; use strict;' then perl would have told
you that the array @clientName is undefined.
jue
------------------------------
Date: Thu, 21 Aug 2008 16:20:20 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Help: Reading file and creating an associative array
Message-Id: <m1r68iw263.fsf@dot-app.org>
HerbF@earthlink.net writes:
> while (<TFILE>) {
> @clientRecord = split /{/,$_;
> $user = $clientRecord[0];
> $clientName[$user] = $clientRecord[2];
> }
>
> Later in processing, I seek to use a defined $user to fetch
> $clientName[$user]. I get a null value.
>
> How do I read in the file and set up the associative array?
I don't see anything wrong with your basic approach - read a line at a
time, split() the key and value fields on the appropriate delimiters,
and store the results in your hash.
Have you tried inserting a print() inside the loop, to verify that the
elements of @clientRecord are what you think they should be? I suspect
that the split() isn't splitting where you want it to - but without a
sample of your data to look at, it's impossible to verify that.
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Thu, 21 Aug 2008 16:24:07 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Help: Reading file and creating an associative array
Message-Id: <m1myj6w1zs.fsf@dot-app.org>
Jürgen Exner <jurgenex@hotmail.com> writes:
> HerbF@earthlink.net wrote:
>
>> $clientName[$user] = $clientRecord[2];
>
> I believe you simply using the wrong type of braces.
D'oh! Can't believe I didn't catch that. :-(
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: 21 Aug 2008 20:36:38 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Help: Reading file and creating an associative array
Message-Id: <6h61umFjb9vaU1@mid.uni-berlin.de>
HerbF@earthlink.net wrote:
> Semi-newbie here. Sorry for posting what is probably a simple question, but
> I'm stumped.
> I want to read in the records of a file and create an associative array with
> the key to each array element being one of the fields of the record and the
> value of each array element being a different field of the same record.
I guess you mean "line" when you write "record", right? And is the
key value always a number? Otherwise you need a hash instead of an
array.
> I'm trying this, but it's not working:
> while (<TFILE>) {
> @clientRecord = split /{/,$_;
> $user = $clientRecord[0];
> $clientName[$user] = $clientRecord[2];
> }
> Later in processing, I seek to use a defined $user to fetch
> $clientName[$user]. I get a null value.
What's a "null value"? And did you use 'use warnings' and 'use strict'?
They may give you important information. And are you sure that all
lines get plit into at least 3 items? Also, if '{' is the first thing
in the line the first element of the array you get from split() will
be empty. And then splitting on '{' looks a bit strange, the only way
the above code could work if all lines start with an integer number,
directly followed by a '{'. It might at least make sense to also
remove white-space around the '{'.
> How do I read in the file and set up the associative array?
Since you don't tell what exactly the input is that's hard to say.
Perhaps something similar to the following gets you a bit further
in your quest:
#!/usr/bin/perl
use warnings;
use strict;
open my $file, '<', 'filename' or die "Can't open 'filename': $!\n";
my %clientName;
while ( my line = <$file> ) {
my @list = split /\s*{\s*/, $line;
unless ( @list >= 3 and $list[ 0 ] ne '' ) {
print "Line $. of input file looks fishy\n";
next;
}
if ( exists $clientName{ $list[ 0 ] } ) {
print "User '$list[ 0 ]' is redefined at line $.\n";
next;
}
$clientName{ $list[ 0 ] } = $list[ 1 ];
}
close $file;
print $_ . ' => ' . $clientName{ $_ } . "\n"
for keys %clientName;
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 21 Aug 2008 18:20:36 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <n6cra41m7dil6e1u7ls622ajbnlj2vnbnf@4ax.com>
Morfys <morfysster@gmail.com> wrote:
>When running perl with the -w option, and executing a "print'" with
>about 30 variables, I get
>
>Use of uninitialized value in print at at ./temp.pl line 9.
>
>Is there any way to make perl report the exact variable name (out of
>the 30) that is not initialized?
>For various reasons, writings 30 statements of the form
>defined ($var) or warn "var is undefined";
>is difficult/a pain in my case.
If it's for debugging purposes only then it takes only 5 attempts using
bisection.
Put 15 of those variables into one print() statement. If this statement
pops the error, then you know it's one of those 15, otherwise it's one
of those 15 you didnt' include. Cut that number in half and use 8 of the
remaining suspects in this print() statement and repeat the process to
reduce the number further. After 5 iterations you've nailed the bad guy.
jue
------------------------------
Date: Thu, 21 Aug 2008 21:50:04 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <g8ko68.1to.1@news.isolution.nl>
Morfys schreef:
> When running perl with the -w option, and executing a "print'" with
> about 30 variables, I get
Just put them in a more appropriate data structure, see perldata.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 21 Aug 2008 20:45:56 GMT
From: xhoster@gmail.com
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <20080821164559.125$5C@newsreader.com>
Jürgen Exner <jurgenex@hotmail.com> wrote:
> Morfys <morfysster@gmail.com> wrote:
> >When running perl with the -w option, and executing a "print'" with
> >about 30 variables, I get
> >
> >Use of uninitialized value in print at at ./temp.pl line 9.
> >
> >Is there any way to make perl report the exact variable name (out of
> >the 30) that is not initialized?
> >For various reasons, writings 30 statements of the form
> >defined ($var) or warn "var is undefined";
> >is difficult/a pain in my case.
>
> If it's for debugging purposes only then it takes only 5 attempts using
> bisection.
> Put 15 of those variables into one print() statement. If this statement
> pops the error, then you know it's one of those 15, otherwise it's one
> of those 15 you didnt' include. Cut that number in half and use 8 of the
> remaining suspects in this print() statement and repeat the process to
> reduce the number further. After 5 iterations you've nailed the bad guy.
I'd do it in fewer attempts by n-section rather than bisection.
First I'd look at the output and see which position had an empty string.
Unless there is more than one such, then the one that is empty is the
one that is undef.
Second, I'd copy the print statement and change it to a foreach:
print "$list,$of,$variables,$one,$of,$which,$is,$undefined\n";
Becomes
my $i=1;
foreach ($list,$of,$variables,$one,$of,$which,$is,$undefined) {
warn "It's the $i th one" unless defined $_;
$i++;
};
But zeroth, I almost certainly avoid having 30 variables explicitly in a
print statement, by using a different data structure.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Thu, 21 Aug 2008 21:20:10 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <kvmra4pbr5dvbqe228rtqvan3ebhfdl0l1@4ax.com>
xhoster@gmail.com wrote:
>But zeroth, I almost certainly avoid having 30 variables explicitly in a
>print statement, by using a different data structure.
Amen to that!
jue
------------------------------
Date: Thu, 21 Aug 2008 23:49:52 +0200
From: nntpman68 <news1234@free.fr>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <48ade300$0$18756$426a74cc@news.free.fr>
> Is there any way to make perl report the exact variable name (out of
> the 30) that is not initialized?
The problem can be reduced by choosing better data structures and
debugging can be done by bi-secting or n-secting a print statements,
Something which I do also sometimes is a statement like this
my @tmp = ($a,$b,$c); # who is not defined ?
print((map { defined $_ ? 1 : 0 } (@tmp)),"\n");
You'll get a 1 for each defined and a 0 for each undefined variable.
normally I just copy the offending line, remove all but the variable names,
But:
Would none of you wish to have a run time error message, that is a
little more explicit.
I don't know how difficult it would be for a future perl version to have
more specific error reporting.
It's probably not that simple, otherwise it would be already there.
Just imagine an error message saying:
"uninitialized value in print at line 10 ($a was not initialized)
I think it would increase productivity.
Does anybody know how perl 6 would react?
bye
N
Morfys wrote:
> Hi,
>
> When running perl with the -w option, and executing a "print'" with
> about 30 variables, I get
>
> Use of uninitialized value in print at at ./temp.pl line 9.
>
> Is there any way to make perl report the exact variable name (out of
> the 30) that is not initialized?
>
> ' use diagnostics;' doesn't help.
>
> For various reasons, writings 30 statements of the form
> defined ($var) or warn "var is undefined";
> is difficult/a pain in my case.
>
> Thanks.
------------------------------
Date: Thu, 21 Aug 2008 23:27:42 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <un61o5-knd.ln1@osiris.mauzo.dyndns.org>
Quoth news1234@free.fr:
>
> But:
> Would none of you wish to have a run time error message, that is a
> little more explicit.
>
> I don't know how difficult it would be for a future perl version to have
> more specific error reporting.
>
> It's probably not that simple, otherwise it would be already there.
As I said, this has already gone into 5.10.
Ben
--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp. [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live. ben@morrow.me.uk
------------------------------
Date: Thu, 21 Aug 2008 19:03:54 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: how to report which specific variable is uninitialized
Message-Id: <slrngas0ja.rco.tadmc@tadmc30.sbcglobal.net>
Morfys <morfysster@gmail.com> wrote:
> On Aug 21, 6:53 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
>> Morfys <morfyss...@gmail.com> writes:
>> > When running perl with the -w option, and executing a "print'" with
>> > about 30 variables, I get
>>
>> > Use of uninitialized value in print at at ./temp.pl line 9.
>>
>> > Is there any way to make perl report the exact variable name (out of
>> > the 30) that is not initialized?
>>
>> > ' use diagnostics;' doesn't help.
>>
>> Are you using strict? That requires you to declare your variables
>> before using them, so it will tell you if the "unitialized"
>> variable is really a variable whose name you've misspelled in the
>> call to print().
>
> Yes, I am using strict. The variables have all been declared.
>
>> > For various reasons, writings 30 statements of the form
>> > defined ($var) or warn "var is undefined";
>> > is difficult/a pain in my case.
>>
>> Honestly, I don't see why. Sure, it would be a pain to write all that
>> boilerplate all the time, but doing it once for debugging purposes
>> shouldn't be too much of a chore.
>
> Ok, I just thought there might some cool way to do it in perl with one
> line. ;)
If you had chosen a better data structure to start with, then there
would some cool way to do it with two lines.
foreach my $varname ( sort keys %vars ) {
print "'$varname' is not defined\n" unless defined $vars{$varname};
}
But then you would have had to use hash keys in place of variable names.
Instead of
$sales_tax = stuff();
use
$vars{sales_tax} = stuff();
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 21 Aug 2008 15:14:23 +0200
From: "Petr Vileta \(fidokomik\)" <stoupa@practisoft.cz>
Subject: Re: OT news server
Message-Id: <g8l17e$2ll8$1@adenine.netfront.net>
Sherm Pendley wrote:
> fidokomik <fidokomik@gmail.com> writes:
>
>> Please can anybody recommend me some news server?
>
> Free or paid? For free, I've had good results from aioe.org. They have
> a fairly short retention period and don't carry binaries, but what
> else can one expect for free? :-)
>
TEST
I found one - freenews.netfront.net
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail from
another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>
-- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
------------------------------
Date: Thu, 21 Aug 2008 22:34:27 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: printing two consecutive lines
Message-Id: <Xns9B01BCF0996E4asu1cornelledu@127.0.0.1>
Sherm Pendley <spamtrap@dot-app.org> wrote in
news:m18wuqgx3d.fsf@dot-app.org:
> cartercc <cartercc@gmail.com> writes:
>
>> Set a flag that by default is false, turns to true when it hits
>> 'Endpoint:' and turns to false when it hits the next 'Beginpoint:'
>> and print if $flag
>>
>> #!/usr/bin/perl
>> use strict;
>> my $flag = 0;
>>
>> open INFILE, "<test.txt" or die "Can't open: $!";
>> while (<INFILE>)
>> {
>> if ($_ =~ /^Endpoint:/ and $flag == 0) {$flag = 1; }
>> elsif ($_ =~ /Beginpoint:/ and $flag == 1) {$flag = 0; }
>> print if $flag;
>> }
>
> Why reinvent the wheel? Perl already has a built-in flip-flop operator
> that does exactly what you describe:
>
> while (<INFILE>) {
> print if ( /^Endpoint:/ .. /^Beginpoint:/ );
> }
While I do not like cartercc's proposed solution, using the range
operator does result in lines beginning with Beginpoint: to be printed
as well.
If that is not desired, the body of the loop needs to be changed to:
print if /^Endpoint:/ .. /^Beginpoint:/ and not /^Beginpoint:/;
Of course, cartercc's solution can be stated more succinctly as:
my $flag;
while ( <DATA> ) {
not $flag and /^Endpoint:/ and $flag = 1;
$flag and /^Beginpoint:/ and $flag = 0;
print if $flag;
}
As usual, beauty is in the eye of the beholder.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Thu, 21 Aug 2008 21:01:05 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: printing two consecutive lines
Message-Id: <m14p5d278u.fsf@dot-app.org>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> writes:
> As usual, beauty is in the eye of the beholder.
I guess you never played D&D... Beholders are *ugly*! :-)
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Thu, 21 Aug 2008 23:26:24 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Reading file and creating an associative array
Message-Id: <g8ktjk.178.1@news.isolution.nl>
HerbF@earthlink.net schreef:
> I want to read in the records of a file and create an associative
> array with the key to each array element being one of the fields of
> the record and the value of each array element being a different
> field of the same record.
>
> I'm trying this, but it's not working:
>
> while (<TFILE>) {
> @clientRecord = split /{/,$_;
> $user = $clientRecord[0];
> $clientName[$user] = $clientRecord[2];
> }
>
> Later in processing, I seek to use a defined $user to fetch
> $clientName[$user]. I get a null value.
>
> How do I read in the file and set up the associative array?
Read perldata. In Perl, an associative array is called a hash.
#/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %clientname;
my $filename = "clients.txt";
{
open my $fh, "<", $filename or die
"Error with $filename: $!";
while ( <$fh> ) {
chomp;
my ($user, undef, $name) = split /\{/;
$clientname{$user} = $name;
}
}
print Dumper( \%clientname );
__END__
Your split-call says that the FS (field separator) is a single left
brace. That seems strange.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Thu, 21 Aug 2008 16:33:48 -0700
From: Ed Jay <edMbj@aes-intl.com>
Subject: Re: Reading file and creating an associative array
Message-Id: <8iura41f705rtj471llaqhbq8e0lm9j4lm@4ax.com>
Dr.Ruud wrote:
>HerbF@earthlink.net schreef:
>
>> I want to read in the records of a file and create an associative
>> array with the key to each array element being one of the fields of
>> the record and the value of each array element being a different
>> field of the same record.
>>
>> I'm trying this, but it's not working:
>>
>> while (<TFILE>) {
>> @clientRecord = split /{/,$_;
>> $user = $clientRecord[0];
>> $clientName[$user] = $clientRecord[2];
>> }
>>
>> Later in processing, I seek to use a defined $user to fetch
>> $clientName[$user]. I get a null value.
>>
>> How do I read in the file and set up the associative array?
>
>Read perldata. In Perl, an associative array is called a hash.
>
>#/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my %clientname;
>
> my $filename = "clients.txt";
> {
> open my $fh, "<", $filename or die
> "Error with $filename: $!";
>
> while ( <$fh> ) {
> chomp;
> my ($user, undef, $name) = split /\{/;
> $clientname{$user} = $name;
> }
> }
> print Dumper( \%clientname );
>__END__
>
Why so much code to replace
@clientRecord = split /{/,$_;
$clientName{$clientRecord[0]} = $clientRecord[2];
>Your split-call says that the FS (field separator) is a single left
>brace. That seems strange.
Yes, but I use commas within the fields to delimit other collections of
strings, e.g., x{y,1,2,3{y{z{4,5,6{
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
------------------------------
Date: Thu, 21 Aug 2008 16:34:12 -0700
From: Ed Jay <edMbj@aes-intl.com>
Subject: Re: Reading file and creating an associative array
Message-Id: <7rura4dk0uj4ghigtn9c561cf9rcq7clnj@4ax.com>
Thanks to all who replied.
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
------------------------------
Date: Thu, 21 Aug 2008 13:30:23 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: set default value for undefined variables
Message-Id: <86myj66x1c.fsf@lifelogs.com>
On Thu, 21 Aug 2008 09:56:17 -0700 (PDT) Morfys <morfysster@gmail.com> wrote:
M> Is there a way to set the default value for undefined variables? For
M> example, in
M> my ($a, $b) = (split(/\s+/, $mytext))[0,1]
M> sometimes mytext will contain two (or more) words, and sometimes it
M> will contain only 1. In the latter case, I would like $b to be
M> initialized to "" not undef.
M> I have many statements (>60) of the above form with different
M> variables, and initializing
M> each variable $a = ""; b = ""; is a pain. Thanks.
You can do
my @data = split(/\s+/, $mytext);
push @data, "" while scalar @data < 2;
If you have so many statements, you need to abstract the operation. Use
a hash or an array to store the results logically. If you show more of
your repetitions we can suggest better ways to organize your code.
Ted
------------------------------
Date: Thu, 21 Aug 2008 18:26:56 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: set default value for undefined variables
Message-Id: <3kcra4tilgki9n9hmupd0vqg98ku9j86rp@4ax.com>
Morfys <morfysster@gmail.com> wrote:
>Is there a way to set the default value for undefined variables? For
>example, in
>
>my ($a, $b) = (split(/\s+/, $mytext))[0,1]
>
>sometimes mytext will contain two (or more) words, and sometimes it
>will contain only 1. In the latter case, I would like $b to be
>initialized to "" not undef.
One way: supply an additional optional element for the array slice, such
that the slice always has at least 2 elements to work with, even if
split() returns only one:
my ($a, $b) = (split(/\s+/, $mytext), '')[0,1]
>I have many statements (>60) of the above form with different
>variables, and initializing
>each variable $a = ""; b = ""; is a pain. Thanks.
Maybe
my ($a, $b) = ('', '');
jue
------------------------------
Date: Thu, 21 Aug 2008 23:58:09 +0200
From: nntpman68 <news1234@free.fr>
Subject: Re: set default value for undefined variables
Message-Id: <48ade4f1$0$9199$426a74cc@news.free.fr>
Morfys wrote:
> Is there a way to set the default value for undefined variables? For
> example, in
>
> my ($a, $b) = (split(/\s+/, $mytext))[0,1]
>
> sometimes mytext will contain two (or more) words, and sometimes it
> will contain only 1. In the latter case, I would like $b to be
> initialized to "" not undef.
>
> I have many statements (>60) of the above form with different
> variables, and initializing
> each variable $a = ""; b = ""; is a pain. Thanks.
>
What about:
my ($a,$b) = (split(' ',$text),'');
or for more values
my ($a,$b,$c,$d,$e) = (split(' ',$text),('')x4);
bye
N
------------------------------
Date: Thu, 21 Aug 2008 21:22:09 +0000 (UTC)
From: Martin Gregorie <martin@see.sig.for.address.invalid>
Subject: Re: The Importance of Terminology's Quality
Message-Id: <g8kma0$e5q$2@localhost.localdomain>
On Thu, 21 Aug 2008 09:11:48 -0500, Rob Warnock wrote:
> You're assuming that all machines *have* some sort of "boot ROM". Before
> the microprocessor days, that was certainly not always the case. The
> "boot ROM", or other methods of booting a machine without manually
> entering at least a small amount of "shoelace" code [enough the *load*
> the real bootstrap], was a fairly late invention.
>
Quite. I never knew how to boot the Elliott 503 (never got closer to the
console than the other side of a plate glass window). However, I dealt
with that aspect of ICL 1900s. They had ferrite core memory and NO ROM.
When you hit Start this cleared the memory and then pulsed a wire that
wrote the bootstrap into memory and executed it. The wire wove through
the cores and wrote 1 bits to the the right places to:
- set word 8 (the PC) to 20
- set 25 words from 20 as bootstrap instructions to boot from disk.
Then it started the CPU running.
On the 1902 this sequence often didn't work, so a good operator knew the
25 words by heart and would toggle them in on hand switches, set PC to 20
and hit the GO switch.
>
> -Rob
>
> p.s. Similarly, the DEC PDP-8 & PDP-11 were also originally booted by
> manually toggling the console switches in order to deposit a few
> instructions into memory, and then the starting address was toggled in
> and "Start" was pushed. It was only later that a boot ROM became
> available for the PDP-11 (as an expensive option!) -and only much later
> still for the PDP-8 series (e.g., the MI8E for the PDP-8/E).
>
> -----
> Rob Warnock <rpw3@rpw3.org>
> 627 26th Avenue <URL:http://rpw3.org/> San Mateo,
CA 94403
> (650)572-2607
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
------------------------------
Date: Thu, 21 Aug 2008 19:27:24 -0400
From: =?ISO-8859-1?Q?Arne_Vajh=F8j?= <arne@vajhoej.dk>
Subject: Re: The Importance of Terminology's Quality
Message-Id: <48adf9d2$0$90269$14726298@news.sunsite.dk>
Piet van Oostrum wrote:
>>>>>> Arne Vajhøj <arne@vajhoej.dk> (AV) wrote:
>> AV> Robert Maas, http://tinyurl.com/uh3t wrote:
>>>> John W Kennedy <jwke...@attglobal.net> wrote:
>> JWK> Into the 60s, indeed, there were still machines being made
>> JWK> that had no instruction comparable to the mainframe BASx/BALx
>> JWK> family, or to Intel's CALL. You had to do a subprogram call by
>> JWK> first overwriting the last instruction of what you were
>> JWK> calling with a branch instruction that would return back to
>> JWK> you.
>>>> That's not true, that you needed to do that, that there was no
>>>> other way available. The subroutine linkage I invented for S.P.S.
>>>> (Symbolic Programming System, i.e. IBM 1620 assembly language) was
>>>> to reserve a 5-digit space immediately before the subroutine entry
>>>> point for storing the return address. So the caller needed to know
>>>> only one address, the entry point, and do both store-return-address
>>>> and jump relative to that address, rather than needing to know both
>>>> the entry point and the last-instruction-JUMP-needs-patch address
>>>> as independent items of information.
>
>> AV> CDC Cyber did something very similar.
>> AV> Not very recursion friendly.
>
> Actually, the CYBER way wasn't too bad. IIRC the CYBER had a subroutine
> instruction that stored the return address in the location that the
> instruction referenced and then jumped to the address following that
> location. To implement a recursive procedure you started the code of the
> procedure with saving the return address to a stack.
It was of course doable.
Else Pascal would have been hard to implement.
Arne
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1811
***************************************