[19065] in Perl-Users-Digest
Perl-Users Digest, Issue: 1260 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jul 7 00:05:36 2001
Date: Fri, 6 Jul 2001 21:05:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <994478712-v10-i1260@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 6 Jul 2001 Volume: 10 Number: 1260
Today's topics:
Best way to pad a string with spaces?? <miscellaneousemail@yahoo.com>
Re: Best way to pad a string with spaces?? <bwalton@rochester.rr.com>
Re: Best way to pad a string with spaces?? <qumsieh@sympatico.ca>
Re: Best way to pad a string with spaces?? <dbe@wgn.net>
Re: Create unique file in dir? <goldbb2@earthlink.net>
Re: DB_File saves records with huge amount of white spa <bwalton@rochester.rr.com>
Re: Error - Undefined subroutine &Win32::Process::Creat <minutiae@usa.net>
Re: Flags to use with DB_File tie call?? <dbe@wgn.net>
Re: handling a file with a perl script <dbe@wgn.net>
Re: How to delete consecutive blank lines <james.mctiernan@rcn.com>
IO-1.20 Won't install from RH7.1 (Xeno Campanoli)
Re: Perl Power Tools or Cygwin? (Alan Barclay)
Re: Placing a variable's contents into an array (Tad McClellan)
Re: problem with data.. <james@zephyr.org.uk>
Re: Required libraries don't appear to be included <goldbb2@earthlink.net>
Re: Using a hash passed by reference inside a function? <jkeen@concentric.net>
Re: Using a hash passed by reference inside a function? <miscellaneousemail@yahoo.com>
Re: Using a hash passed by reference inside a function? <dbe@wgn.net>
Why for loop not working?? <miscellaneousemail@yahoo.com>
Re: Why for loop not working?? <uri@sysarch.com>
Re: Why for loop not working?? <jkeen@concentric.net>
Re: Why for loop not working?? <miscellaneousemail@yahoo.com>
Re: Why for loop not working?? <dbe@wgn.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 07 Jul 2001 02:45:05 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Best way to pad a string with spaces??
Message-Id: <MPG.15b025632ecd515c98968d@news.edmonton.telusplanet.net>
Hi everyone,
What is the best way to pad a string with spaces? Such that I can nicely
line up the values contained in them. Or perhaps there is a way to do it
without having to pad the string variables.
Something like the following where the variables would be nicely aligned
if I was able to use a format (which doesn't work because formats are
meant to be used with open file handles)...
print_unpacked(\%h);
format RECORD =
$first_name , $date_started , $tos_version
.
sub print_unpacked
{
my ($hash) = @_;
my ($first_name, $date_started, $tos_version);
while (($k, $v) = each %$hash)
{
print $k.",";
($first_name, $date_started, $tos_version) = split(/,/,$v);
write (RECORD);
}
}
$h is a hash containing name / value pairs in the following structure:
<email_address>,<"first_name,date_started,tos_version">
In the code above $k would correspond to email_address while $v would
contain a the string "first_name,date_started,tos_version".
Any ideas??
--
Carlos
www.internetsuccess.ca
------------------------------
Date: Sat, 07 Jul 2001 03:33:12 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Best way to pad a string with spaces??
Message-Id: <3B468029.679C3C77@rochester.rr.com>
"Carlos C. Gonzalez" wrote:
...
> What is the best way to pad a string with spaces? Such that I can nicely
> line up the values contained in them. Or perhaps there is a way to do it
> without having to pad the string variables.
>
> Something like the following where the variables would be nicely aligned
> if I was able to use a format (which doesn't work because formats are
> meant to be used with open file handles)...
>
> print_unpacked(\%h);
> format RECORD =
> $first_name , $date_started , $tos_version
> .
> sub print_unpacked
> {
> my ($hash) = @_;
> my ($first_name, $date_started, $tos_version);
> while (($k, $v) = each %$hash)
> {
> print $k.",";
> ($first_name, $date_started, $tos_version) = split(/,/,$v);
> write (RECORD);
> }
> }
>
> $h is a hash containing name / value pairs in the following structure:
>
> <email_address>,<"first_name,date_started,tos_version">
>
> In the code above $k would correspond to email_address while $v would
> contain a the string "first_name,date_started,tos_version".
...
> Carlos
...
Check out:
perldoc perlform
particularly the section "Accessing Formatting Internals".
--
Bob Walton
------------------------------
Date: Fri, 6 Jul 2001 23:39:20 -0400
From: "Ala Qumsieh" <qumsieh@sympatico.ca>
Subject: Re: Best way to pad a string with spaces??
Message-Id: <auv17.5407$bs2.646577@news20.bellglobal.com>
"Carlos C. Gonzalez" <miscellaneousemail@yahoo.com> wrote in message
news:MPG.15b025632ecd515c98968d@news.edmonton.telusplanet.net...
> What is the best way to pad a string with spaces? Such that I can nicely
> line up the values contained in them. Or perhaps there is a way to do it
> without having to pad the string variables.
Again, you choose the easy way by posting a question rather than looking up
the answer yourself. Writing the FAQs took up a huge amount of time from
many dedicated individuals specifically to help other people find the
answers themselves. Please attempt to consult them before posting. You'll
make your life easier, and you might stumble upon other gems hidden in the
docs (and believe me, there are a lot).
Keep in mind that asking too many FAQs can put you in many people's kill
files.
As for your question, perlfaq4 has the answer:
% perldoc -q pad
How do I pad a string with blanks or pad a number with zeroes?
--Ala
------------------------------
Date: Fri, 06 Jul 2001 20:57:21 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Best way to pad a string with spaces??
Message-Id: <3B4688A1.64CD8A66@wgn.net>
"Carlos C. Gonzalez" wrote:
>
> Hi everyone,
>
> What is the best way to pad a string with spaces? Such that I can nicely
> line up the values contained in them. Or perhaps there is a way to do it
> without having to pad the string variables.
>
> Something like the following where the variables would be nicely aligned
> if I was able to use a format (which doesn't work because formats are
> meant to be used with open file handles)...
>
> print_unpacked(\%h);
> format RECORD =
> $first_name , $date_started , $tos_version
> .
> sub print_unpacked
> {
> my ($hash) = @_;
> my ($first_name, $date_started, $tos_version);
> while (($k, $v) = each %$hash)
> {
> print $k.",";
> ($first_name, $date_started, $tos_version) = split(/,/,$v);
> write (RECORD);
> }
> }
>
> $h is a hash containing name / value pairs in the following structure:
>
> <email_address>,<"first_name,date_started,tos_version">
>
> In the code above $k would correspond to email_address while $v would
> contain a the string "first_name,date_started,tos_version".
>
> Any ideas??
Just use (s)printf with a fixed width field size.
my $line = sprintf "%-20s %-12s %-15s\n", $first_name, $date_started, $tos_version;
20, 12 and 15 are arbitray on my part.
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site)
-/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/
------------------------------
Date: Fri, 06 Jul 2001 23:52:07 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Create unique file in dir?
Message-Id: <3B468767.6BC28297@earthlink.net>
Martien Verbruggen wrote:
>
> On Sun, 01 Jul 2001 08:18:10 -0400,
> Benjamin Goldberg <goldbb2@earthlink.net> wrote:
> > Zur Aougav wrote:
> >>
> >> P.S. Lock and retry (with a new name) is a must in multi-processor
> >> environment. Safe coding (even if you don't have this kind of
> >> environment) is highly recommended.
> >
> > Or better yet, use the O_EXCL flag with sysopen.
> > use Fcntl;
> > sub mktmp {
> > my $a = "/tmp/" . time . "." . $$ . rand . ".";
> > my $b = "0";
> > my $fh;
> > ++$b until sysopen $fh, $a.$b, O_CREAT | O_EXCL | O_RDWR;
> > if( wantarray ) {
> > return ($fh, $a.$b);
> > } else {
> > unlink $a.$b;
> > return $fh;
> > }
> > }
>
> In this case, the file is always created in /tmp, and therefore not on
> an NFS mount. However, the above isn't a general recipe that works in
> all cases.
>
> The use of O_EXCL on NFS mounted file systems can be, and in many
> cases is, very unreliable. In that case, the standard recipe is to use
> a file with a unique name (including hostname and pid, for example) to
> use as a lockfile. However, now we're back to the original problem :)
Hmm, yes, hostname would have been a good idea to have in there.
[Re NFS] Worse, actually, since you need to *agree* on the lockfile.
> > Having the rand() in there makes it highly likely to succeed on the
> > first try, having the counter ($b) makes it so that if it doesn't
> > succeed at first, it will eventually, assuming /tmp/ is writable.
>
> *nod* I've used this approach in multithreaded and multi-process
> applications before. One of them has been in production for years,
I only considered multi-process, not multi-thread... perl doesn't thread
easily; if it did, I would have included thread num as part of $a.
> without ever creating a conflict, but creating many files with names
> that has an incremented serial number, showing me that there would
> have been a conflict if the precautions weren't taken. And this would
> even happen on times that the application had very little to do. The
> point of this last sentence of course being that defensive programming
> like this is not only for those who write high-traffic applications.
> If you leave a chance for collision open, then some day it will
> happen.
Of course... remember, you're not paranoid when the universe really *is*
out to get you :) For that matter, that's why my $b is initialized to
"0" rather than 0... so that it can become as long as necessary...
though I would be rather surprised to have to go that high :)
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 07 Jul 2001 03:47:04 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: DB_File saves records with huge amount of white space in file??
Message-Id: <3B468368.EF872B72@rochester.rr.com>
"Carlos C. Gonzalez" wrote:
...
> I am starting to use DB_File and have noticed that the data I save to a
> file through this module is saved with a huge amount of white space
> between each record.
>
> Does anyone know if this means that DB_File produces huge database files
> as a result?
...
> Carlos
...
Well, DB_File, like any dbm-type file setup, has its overhead. As a
significant number of records are added, the overhead should go down, at
least percentage-wise. Note that deleted records will most likely not
result in a reduced file size. That space will be reclaimed as
additional new records are stored. If you want to "clean up" a dbm-type
file, you can do so by simply copying the hash tied to the old file to
another hash tied to the newly created file, as in %newhash=%oldhash; .
Note also that some of the older dbm-type file setups (including the
original dbm and ndbm) generated files with "holes". These "holes" were
blocks in the file that were not actually allocated, and hence did not
actually exist and take up disk space. Everything would be happy until
someone copied one of these files. At that time, the "holes" would be
filled in with actual allocated blocks in the copy, and the copied file
would become so huge it might fill the filesystem. Most of the newer
implementations don't do that. You will need to read up on the details
of your particular OS's implementation of DB_File to see exactly what
you've got. You might even be able to tweak the settings for optimum
performance for your usage.
--
Bob Walton
------------------------------
Date: Sat, 07 Jul 2001 03:24:37 GMT
From: minutiae <minutiae@usa.net>
Subject: Re: Error - Undefined subroutine &Win32::Process::Create
Message-Id: <3B46840A.D745A2E1@usa.net>
I forgot to print it out here but actually i have got the
use Win32::Process;
Unfortunately, i did have that, any other suggestions ? the use is there in
both the scripts and i tried with both with and without use.
thanks
Simon Flack wrote:
> > I am calling a perl script from another perl script and i get the
> > following error any suggestions as to how to solve it would be great.
> >
>
> [...snip...]
>
> > #!e:\perl\bin\perl -w
> >
> > Win32::Process::Create($ProcessObj,
> > "D:\\Progra~1\\Apache~1\\Apache\\cgi-bin\\some_exe.exe", "s", 0,
> > NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport();
>
> you need to add "use Win32::Process;" to your script.
>
> That the Win32::Process::Create function is part of the Win32::Process
> module that you aren't loading, so perl doesn't know what to do.
>
> Regards
> Simon
>
> "minutiae" <minutiae@usa.net> wrote in message
> news:3B45673A.76B033D5@usa.net...
> >
> > I am calling a perl script from another perl script and i get the
> > following error any suggestions as to how to solve it would be great.
> >
> > Thanks
> >
> >
> > Undefined subroutine &Win32::Process::Create called at D:\Program
> > Files\Apache Group\Apache\cgi-bin\test2.pl line 3.
> >
> > Orig.pl
> > #!e:\perl\bin\perl -w
> >
> > Win32::Process::Create($ProcessObj,
> > "D:\\Progra~1\\Apache~1\\Apache\\cgi-bin\\test.pl", "s", 0,
> > DETACHED_PROCESS, ".")|| die ErrorReport();
> >
> > Test.pl
> >
> > #!e:\perl\bin\perl -w
> >
> > Win32::Process::Create($ProcessObj,
> > "D:\\Progra~1\\Apache~1\\Apache\\cgi-bin\\some_exe.exe", "s", 0,
> > NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport();
> >
> >
> > I call test.pl from orig.pl and i get the error :
> > Undefined subroutine &Win32::Process::Create called at D:\Program
> > Files\Apache Group\Apache\cgi-bin\test2.pl line 3.
> >
> > However if i call only test.pl it executes perfectly fine without any
> > errors. In fact if i change the orig.pl to call an executable instead of
> > test.pl it runs fine. So i am completely lost.
> >
> > Any thoughts would be much appreciated.
> >
> > Thanks.
> >
------------------------------
Date: Fri, 06 Jul 2001 19:18:43 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Flags to use with DB_File tie call??
Message-Id: <3B467183.A125485F@wgn.net>
"Carlos C. Gonzalez" wrote:
>
> Hi everyone,
>
> I have just started using the DB_File Perl module and have some questions
> about it.
>
> Under this module one would normally "open" a file (tiying it to a hash)
> by using the following type of call.
>
> tie %h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_HASH
> or die "Cannot open $file: $!\n";
>
> I have looked in the DB_File documentation, the Berkely DB documentation,
> and in www.perl.com and have not yet found any listing of the values that
> can be passed into the 4rth parameter (which above is O_RDWR|O_CREAT).
> Does anyone know where I can get the various values for this paramenter.
> They are flags of some sort designating how a file will be processed. I
> know that much.
Try Fcntl.pm
------------------------------
Date: Fri, 06 Jul 2001 19:24:20 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: handling a file with a perl script
Message-Id: <3B4672D4.257ABD4A@wgn.net>
Ka-Hing Cheung wrote:
>
> how to hadle a requested file with a certain extension with a perl
> script?
> I do not have access to the httpd.conf, so is there a way to do it in
> .htaccess?
AddType application/x-httpd-cgi .pl .cgi
> if so, do I need to put it in every directory?
As long as it's in dir you are in or somewhere directly above in the tree
where you are it should work.
> also, what will the server pass to my script so that I know which file
> has been requested?
The script is the file being requested.
> What if the file being requested doesnot exist? will the server know
> this and not invoking my script?
You'll get a 404 error.
------------------------------
Date: Fri, 6 Jul 2001 20:03:31 -0700
From: "Jim McTiernan" <james.mctiernan@rcn.com>
Subject: Re: How to delete consecutive blank lines
Message-Id: <9i5ua5$8lu$1@bob.news.rcn.net>
Philip Newton <pne-news-20010706@newton.digitalspace.net> wrote in message
news:rmrbkt8hcfa2gjtbsutu91r0kg9v70sd0j@4ax.com...
> On Thu, 5 Jul 2001 16:31:51 -0700, "Jim McTiernan"
> <james.mctiernan@rcn.com> wrote:
>
> > I have a text file with multiple blank lines before and after
paragraphs.
> > How can I get rid of them?
>
> Read it in with $/ set to "" (paragraph mode) and $\ set to "\n\n",
> chomp each record, and write it out again.
>
> Since chomp removes multiple \n's when in paragraph mode, this will
> normalise the spacing.
>
> > How also to delete paragraphs that contain certain words, or would it be
> > easier to save the paragraphs that contain only certain words.
>
> If you're reading the file in paragraph mode, simply don't print out the
> current record if it contains a certain word.
>
> Cheers,
> Philip
> --
> Philip Newton <nospam.newton@gmx.li>
> That really is my address; no need to remove anything to reply.
> If you're not part of the solution, you're part of the precipitate.
The paragraph mode ( $/ = ""; ) worked great.
Didn't even have to worry about the consecutive blank lines,
and was then able to match on words contained in paragraphs
and print them out!
Thanks
Jim
------------------------------
Date: 7 Jul 2001 02:19:37 GMT
From: xeno@eskimo.com (Xeno Campanoli)
Subject: IO-1.20 Won't install from RH7.1
Message-Id: <9i5rjp$ebh$1@eskinews.eskimo.com>
I get the following diagnostics when doing the "make" step in the IO-1.20
install. Is there a common remedy other than regressing to some previous
version of something? The perl is apparently 5.6.
IO.xs: In function `XS_IO__Seekable_getpos':
IO.xs:208: `sv_undef' undeclared (first use in this function)
IO.xs:208: (Each undeclared identifier is reported only once
IO.xs:208: for each function it appears in.)
IO.xs: In function `XS_IO__File_new_tmpfile':
IO.xs:252: `sv_undef' undeclared (first use in this function)
make: *** [IO.o] Error 1
------------------------------
Date: 7 Jul 2001 01:36:08 GMT
From: gorilla@elaine.furryape.com (Alan Barclay)
Subject: Re: Perl Power Tools or Cygwin?
Message-Id: <994469765.772625@elaine.furryape.com>
In article <Pine.SUN.4.33.0107061634420.10750-100000@mamba.cs.Virginia.EDU>,
David Coppit <newspost@coppit.org> wrote:
>So I'm thinking of adding some Unix-style tools to my Windows arsenal.
>Cygwin seems like an obvious choice, but then there's the Perl Power
>Tools project: http://language.perl.com/ppt/, which seems to have
>implementations for a lot of stuff already...
>
>Anyone using PPT exclusively? Any comments/advice? (Obviously you
>don't get bash, which may be enough to make me want Cygwin. :)
I've not used PPT exclusively, but do use cygwin, and I have used the
PPT tools on odd occasions.
Most of them are fine. However, some, due to their perl implementation,
are significantly slower than the cygwin equvilants. This may or may
not be a problem for you.
------------------------------
Date: Fri, 6 Jul 2001 14:04:29 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Placing a variable's contents into an array
Message-Id: <slrn9kbvdd.ket.tadmc@tadmc26.august.net>
Evan Panagiotopoulos <epanagio@frontiernet.net> wrote:
>I have a variable which contains the output of a grep command.
^^^^^^^^ ^^^^^^^^^^^^
1) what kind of variable? Scalar? Array? Hash?
2) which grep? grep(1) (unix) or Perl's grep() function?
>How can I
>place portions of that string into an array?
If the "string" contains embedded newlines and you want one
line per array element, then:
my @greplines = split /\n/, $string;
If you want something else, then please try rephrasing your
question so that it is not so ambiguous. We cannot tell what
your really have, nor where you want it to go...
Real Perl Code is unambiguous. Show us the Perl code that
loads up the "variable" you are speaking of.
If you want it in an array, why did you put it into a single
scalar in the first place? You can arrange for either of the
greps to put their results directly into an array for you.
>Can I get perl to place the
>first 15 characters into the first element of an array.
$greplines[0] = substr $string, 0, 15;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 7 Jul 2001 01:50:03 +0100
From: James Coupe <james@zephyr.org.uk>
Subject: Re: problem with data..
Message-Id: <Qon0H+A7ylR7EwNK@gratiano.zephyr.org.uk>
In message <NUr17.152790$qv3.42719635@nnrp5-w.sbc.net>, Darren Dunham
<ddunham@redwood.taos.com> writes
>Anyone see what (if anything) I've done wrong here?
Tangentially:
Might I suggest that scp might be an equally valid method of doing this,
which simply requires issuing the scp commands? Laziness, impatience,
hubris etc. (And in my case, stupidity.)
--
James Coupe PGP Key: 0x5D623D5D
EBD690ECD7A1F
It's me Nono small robot you know - friend of Ulysses B457CA213D7E6
It's me Nono small robot you know - friend of Ulysses 68C3695D623D5D
------------------------------
Date: Fri, 06 Jul 2001 23:28:39 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Required libraries don't appear to be included
Message-Id: <3B4681E7.AB09C10C@earthlink.net>
Sean wrote:
>
> Hello.
>
> I have a UBB script that I moved from one server to another. The main
> scripts work fine, but one section does not work on the new server.
>
> It appears that the required libraries are not being included as many
> variables that are set in those libraries are evaluating to blank when
> the script runs.
blank? The empty string? Or are they undefined?
>
> Here is one of the requires:
>
> eval {
>
> ($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script
> location: UNIX / or Windows /
> ($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script
> location: Windows \
> require "UltBB.setup";
>
> };
>
> Can someone give me some thoughts on why these files wouldn't be
> included, or if they are included why the variables are evaluating
> correctly?
Possibly, the file isn't being included because it isn't being found.
I would do:
use FindBin;
use lib $FindBin::Bin;
require "UltBB.setup";
I would *not* wrap it in an eval, unless I want to ignore any errors
there might be in the "UltBB.setup" file.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 07 Jul 2001 02:37:20 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: Using a hash passed by reference inside a function??
Message-Id: <9i5sl0$2tq@dispatch.concentric.net>
"Carlos C. Gonzalez" <miscellaneousemail@yahoo.com> wrote in message
news:MPG.15afd4838cbee8d989687@news.edmonton.telusplanet.net...
> Hi ya'll,
>
> How can I use a hash passed by reference (in order to prevent it being
> flattened into a scalar) inside a subroutine?
>
> Say that I have a hash called %h
>
> Say that I pass that hash by a call to a function called contents like
> so:
>
> print contents(\%h);
>
> How do I get the function as listed below to print out the hash passed by
> reference??
>
> sub contents
> {
> my $hash = @_;
> while (($k, $v) = each $hash)
>
> print "$k -> $v\n"
> }
> }
>
> Perl tells me that I the argument to each must be a hash and not a
> private variable.
>
You should declare $k and $v as "my" variables. $hash inside the sub is a
hash reference. You need to de-reference it in order to apply "each". See
re-coding below.
use strict;
my %hash = ( color => 'blue',
time => 'evening',
country => 'usa',);
contents(\%hash);
sub contents
{ my $hashref = shift;
my ($k, $v);
while (($k, $v) = each %$hashref)
{ print "$k -> $v\n" }
}
------------------------------
Date: Sat, 07 Jul 2001 03:36:26 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Using a hash passed by reference inside a function??
Message-Id: <MPG.15b0315aae4bad3e98968e@news.edmonton.telusplanet.net>
In article <9i5sl0$2tq@dispatch.concentric.net>, jkeen@concentric.net
says...
>
> use strict;
>
> my %hash = ( color => 'blue',
> time => 'evening',
> country => 'usa',);
>
> contents(\%hash);
>
> sub contents
> { my $hashref = shift;
> my ($k, $v);
> while (($k, $v) = each %$hashref)
> { print "$k -> $v\n" }
> }
I understand everything in the code above (I think) except for the line
"my $hashref = shift". I was wondering if you could explain that to me.
I think it means...
assign the value of $_[1] to $hashref?? $_[1] being the first value in
@_ after the address of %hash. $_[0] being the address of %hash.
Is that right?
Another thing I don't understand James is why one can access a scalar
such as $hashref by putting the hash operator in front of it? Can every
scalar be referenced as a hash by putting the % in front of it? Such as
%$<scalar_name>?
Any more input on this would be appreciated.
--
Carlos
www.internetsuccess.ca
------------------------------
Date: Fri, 06 Jul 2001 20:51:09 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Using a hash passed by reference inside a function??
Message-Id: <3B46872D.842CCCCC@wgn.net>
"Carlos C. Gonzalez" wrote:
>
> In article <9i5sl0$2tq@dispatch.concentric.net>, jkeen@concentric.net
> says...
> >
> > use strict;
> >
> > my %hash = ( color => 'blue',
> > time => 'evening',
> > country => 'usa',);
> >
> > contents(\%hash);
> >
> > sub contents
> > { my $hashref = shift;
> > my ($k, $v);
> > while (($k, $v) = each %$hashref)
> > { print "$k -> $v\n" }
> > }
>
> I understand everything in the code above (I think) except for the line
> "my $hashref = shift". I was wondering if you could explain that to me.
> I think it means...
>
> assign the value of $_[1] to $hashref?? $_[1] being the first value in
> @_ after the address of %hash. $_[0] being the address of %hash.
>
> Is that right?
No, there is no $_[1] since only one arg is passed (a reference to %hash).
The shift will take $_[0] (the lone arg) and put it in $hashref.
> Another thing I don't understand James is why one can access a scalar
> such as $hashref by putting the hash operator in front of it?
The %$hashref is dereferencing the hash ref back to a hash. Try reading the
perlreftut man page/pod.
> Can every
> scalar be referenced as a hash by putting the % in front of it? Such as
> %$<scalar_name>?
Wouldn't make sense unles it was a hash ref to start with.
------------------------------
Date: Sat, 07 Jul 2001 01:48:53 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Why for loop not working??
Message-Id: <MPG.15b0181c6a03fe98968b@news.edmonton.telusplanet.net>
Hi everyone,
Why does the following code not work??
use strict;
my $s = "carlos,carlos\@somedomain.com,07-05-2001";
my @temp = split(/,/,$s);
for (my $i=0; @temp; $i++)
{
print $temp[$i]."\n";
}
I know that the correct values are split and assigned to @temp. When I
use the following code it works just fine in terms of printing out the
three values contained in @temp, three times.
use strict;
my $s = "carlos,carlos\@somedomain.com,07-05-2001";
my @temp = split(/,/,$s);
for (@temp)
{
print $temp[0]."\n";
print $temp[1]."\n";
print $temp[2]."\n";
}
Why does the first "for" loop above not work. All I get is a flickering
on the screen with no output.
Thanks for any advice on this. I really appreciate your alls help as I
continue to learn to work with Perl.
--
Carlos
www.internetsuccess.ca
------------------------------
Date: Sat, 07 Jul 2001 02:14:26 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Why for loop not working??
Message-Id: <x7zoah4ha5.fsf@home.sysarch.com>
>>>>> "CCG" == Carlos C Gonzalez <miscellaneousemail@yahoo.com> writes:
CCG> Hi everyone,
CCG> Why does the following code not work??
CCG> use strict;
CCG> my $s = "carlos,carlos\@somedomain.com,07-05-2001";
CCG> my @temp = split(/,/,$s);
CCG> for (my $i=0; @temp; $i++)
^^^^^
that is a constant value unless @temp changes. maybe you meant $i <
@temp?
but you should lean toward foreach loops in perl. only
where you need the index (or in special cases) is the c style for loop
used.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: 07 Jul 2001 02:16:50 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: Why for loop not working??
Message-Id: <9i5rei$374@dispatch.concentric.net>
"Carlos C. Gonzalez" <miscellaneousemail@yahoo.com> wrote in message
news:MPG.15b0181c6a03fe98968b@news.edmonton.telusplanet.net...
> Hi everyone,
>
> Why does the following code not work??
>
> use strict;
> my $s = "carlos,carlos\@somedomain.com,07-05-2001";
> my @temp = split(/,/,$s);
> for (my $i=0; @temp; $i++)
> {
> print $temp[$i]."\n";
> }
>
Your "for" loop is missing the test condition from the 2nd element in the
parens. It should be either:
for (my $i=0; $i <= $#temp; $i++)
or
for (my $i=0; $i < scalar(@temp); $i++)
James E. Keenan
jkeen@concentric.net
Brooklyn, NY
------------------------------
Date: Sat, 07 Jul 2001 02:37:52 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Why for loop not working??
Message-Id: <MPG.15b023848b3694b698968c@news.edmonton.telusplanet.net>
In article <9i5rei$374@dispatch.concentric.net>, jkeen@concentric.net
says...
> Your "for" loop is missing the test condition from the 2nd element in the
> parens.
> for (my $i=0; $i <= $#temp; $i++)
Boy oh boy I thought I was starting to get the hang of some of this
*grin*. What does "$i <= $#temp" mean?
I can make out some of it such as...
while the value of $i is less than or equal to ??? (something about
temp).
but how can I compare a numeric value to $#temp?? What does $#temp mean?
--
Carlos
www.internetsuccess.ca
------------------------------
Date: Fri, 06 Jul 2001 20:54:03 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Why for loop not working??
Message-Id: <3B4687DB.4D8AF6D2@wgn.net>
"Carlos C. Gonzalez" wrote:
>
> In article <9i5rei$374@dispatch.concentric.net>, jkeen@concentric.net
> says...
>
> > Your "for" loop is missing the test condition from the 2nd element in the
> > parens.
> > for (my $i=0; $i <= $#temp; $i++)
>
> Boy oh boy I thought I was starting to get the hang of some of this
> *grin*. What does "$i <= $#temp" mean?
>
> I can make out some of it such as...
>
> while the value of $i is less than or equal to ??? (something about
> temp).
>
> but how can I compare a numeric value to $#temp?? What does $#temp mean?
I prefer:
for (my $ii = 0; $ii < @temp; $ii++) {
$#temp is the last array index of @temp. By using @temp instead (in a
scalar context) you get the full array size and can use < instead of <=
for the comparison. If @temp has 6 elements, $# is equal to 5 (the index
of the last element of the array).
------------------------------
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.
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 V10 Issue 1260
***************************************