[16450] in Perl-Users-Digest
Perl-Users Digest, Issue: 3862 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 31 18:16:00 2000
Date: Mon, 31 Jul 2000 15:15:34 -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: <965081734-v9-i3862@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 31 Jul 2000 Volume: 9 Number: 3862
Today's topics:
Rounding numbers in Perl <samara_biz@hotmail.com>
Re: Rounding numbers in Perl (Greg Bacon)
Re: Rounding numbers in Perl (Randal L. Schwartz)
Re: Rounding numbers in Perl <samara_biz@hotmail.com>
Re: Rounding numbers in Perl <samara_biz@hotmail.com>
Re: Rounding numbers in Perl (Greg Bacon)
Re: Rounding numbers in Perl <dperham@dperham.eng.tvol.net>
Re: Rounding numbers in Perl <tina@streetmail.com>
Re: Rounding numbers in Perl <rschram@reed.edu>
Re: Rounding numbers in Perl (Greg Bacon)
Re: Rounding numbers in Perl (Logan Shaw)
SCP instead of FTP? <slavelle@concentus-tech.com>
select/vec/pipes question <margit@us.ibm.com>
Re: simple regex? (Mark-Jason Dominus)
Re: simple regex? pcj1@my-deja.com
Re: simple regex? (Greg Bacon)
splitting on spaces emavres@my-deja.com
Re: splitting on spaces ericr@yankthechain.com
Re: splitting on spaces <sariq@texas.net>
Re: splitting on spaces (Greg Bacon)
Re: splitting on spaces <red_orc@my-deja.com>
Re: splitting on spaces ericr@yankthechain.com
Re: splitting on spaces <tina@streetmail.com>
Re: string -> array? <bart.lateur@skynet.be>
Re: string -> array? <lr@hpl.hp.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 31 Jul 2000 14:18:17 -0400
From: "Alex T." <samara_biz@hotmail.com>
Subject: Rounding numbers in Perl
Message-Id: <3985C2E9.361A93DB@hotmail.com>
Hi,
I need to store the sizes of files in an array. The sizes should be
shown in kilobytes. I get file size in kilobytes as shown below:
$filesize = stat($file_name)[7] / 1024;
I want to round filesize to one digit after the point and I am not sure
how to do it. I know print command allows to format the output string,
but I need to do assignment instead of printing a variable to the
screen.
Could anyone point me to where I can find out how to round numbers in
Perl or give me a simple example?
Thanks!!!
Alex
------------------------------
Date: Mon, 31 Jul 2000 18:59:31 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Rounding numbers in Perl
Message-Id: <sobj4jqn9ft98@corp.supernews.com>
In article <3985C2E9.361A93DB@hotmail.com>,
Alex T. <samara_biz@hotmail.com> wrote:
: I need to store the sizes of files in an array. The sizes should be
: shown in kilobytes. I get file size in kilobytes as shown below:
:
: $filesize = stat($file_name)[7] / 1024;
:
: I want to round filesize to one digit after the point and I am not sure
: how to do it. I know print command allows to format the output string,
: but I need to do assignment instead of printing a variable to the
: screen.
Go back and reread the first line of the perlfunc documentation on
the printf operator.
Greg
--
To me vi is Zen. To use vi is to practice zen. Every command is a koan.
Profound to the user, unintelligible to the uninitiated. You discover
truth everytime you use it.
-- reddy@lion.austin.ibm.com
------------------------------
Date: 31 Jul 2000 12:07:35 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Rounding numbers in Perl
Message-Id: <m13dkqt8y0.fsf@halfdome.holdit.com>
>>>>> "Alex" == Alex T <samara_biz@hotmail.com> writes:
Alex> I need to store the sizes of files in an array. The sizes should be
Alex> shown in kilobytes. I get file size in kilobytes as shown below:
Alex> $filesize = stat($file_name)[7] / 1024;
I don't think you got it that way. I think you might have done
something like:
$filesize = (stat $filename)[7] / 1024;
Alex> I want to round filesize to one digit after the point and I am not sure
Alex> how to do it. I know print command allows to format the output string,
Alex> but I need to do assignment instead of printing a variable to the
Alex> screen.
Alex> Could anyone point me to where I can find out how to round numbers in
Alex> Perl or give me a simple example?
Perhaps the PerlFAQ already answers that. "perldoc perlfaq" for details.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Mon, 31 Jul 2000 15:27:48 -0400
From: "Alex T." <samara_biz@hotmail.com>
Subject: Re: Rounding numbers in Perl
Message-Id: <3985D333.C2F51793@hotmail.com>
It says "printf FILEHANDLE FORMAT, LIST"?
I don't see how that answers my question. I read documentation on how printf
works (before the first posting) and I don't see how it could help me.
Could you give me a hint?
Thanks!
Alex
Greg Bacon wrote:
> In article <3985C2E9.361A93DB@hotmail.com>,
> Alex T. <samara_biz@hotmail.com> wrote:
>
> : I need to store the sizes of files in an array. The sizes should be
> : shown in kilobytes. I get file size in kilobytes as shown below:
> :
> : $filesize = stat($file_name)[7] / 1024;
> :
> : I want to round filesize to one digit after the point and I am not sure
> : how to do it. I know print command allows to format the output string,
> : but I need to do assignment instead of printing a variable to the
> : screen.
>
> Go back and reread the first line of the perlfunc documentation on
> the printf operator.
>
> Greg
> --
> To me vi is Zen. To use vi is to practice zen. Every command is a koan.
> Profound to the user, unintelligible to the uninitiated. You discover
> truth everytime you use it.
> -- reddy@lion.austin.ibm.com
------------------------------
Date: Mon, 31 Jul 2000 15:45:47 -0400
From: "Alex T." <samara_biz@hotmail.com>
Subject: Re: Rounding numbers in Perl
Message-Id: <3985D76B.C01190F8@hotmail.com>
FAQ basically tells me that I have to write my own truncating/rounding function.
Did anyone have to deal with truncation to a certain digit after the point in
Perl?
Thanks!
Alex
"Randal L. Schwartz" wrote:
> >>>>> "Alex" == Alex T <samara_biz@hotmail.com> writes:
>
> Alex> I need to store the sizes of files in an array. The sizes should be
> Alex> shown in kilobytes. I get file size in kilobytes as shown below:
>
> Alex> $filesize = stat($file_name)[7] / 1024;
>
> I don't think you got it that way. I think you might have done
> something like:
>
> $filesize = (stat $filename)[7] / 1024;
>
> Alex> I want to round filesize to one digit after the point and I am not sure
> Alex> how to do it. I know print command allows to format the output string,
> Alex> but I need to do assignment instead of printing a variable to the
> Alex> screen.
>
> Alex> Could anyone point me to where I can find out how to round numbers in
> Alex> Perl or give me a simple example?
>
> Perhaps the PerlFAQ already answers that. "perldoc perlfaq" for details.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
> <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Mon, 31 Jul 2000 20:16:56 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Rounding numbers in Perl
Message-Id: <sobnlop09ft100@corp.supernews.com>
[Unjeopardized]
In article <3985D333.C2F51793@hotmail.com>,
Alex T. <samara_biz@hotmail.com> wrote:
: Greg Bacon wrote:
:
: > Go back and reread the first line of the perlfunc documentation on
: > the printf operator.
:
: It says "printf FILEHANDLE FORMAT, LIST"?
A problem of definitions, but the principle of charity applies. You
should have looked around a little to see whether we were using terms
differently. I'd consider that line to be a label and not part of
printf's documentation.
: I don't see how that answers my question. I read documentation on how
: printf works (before the first posting) and I don't see how it could
: help me.
Did you read any of the printf documentation?
printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST
Equivalent to [...]
The first line of the perlfunc documentation on the printf operator
begins with "Equivalent to". Find that mystery equivalence, and you'll
have your answer.
Greg
--
Reading the FAQ is like staying in line--it's something you should
learn in kindergarten. Usenet needs a better kindergarten.
-- Larry Wall in <1995Nov9.193745.13694@netlabs.com>
------------------------------
Date: 31 Jul 2000 16:45:57 -0400
From: Doug Perham <dperham@dperham.eng.tvol.net>
Subject: Re: Rounding numbers in Perl
Message-Id: <81em4am3ju.fsf@wgate.com>
"Alex T." <samara_biz@hotmail.com> writes:
> FAQ basically tells me that I have to write my own truncating/rounding function.
>
> Did anyone have to deal with truncation to a certain digit after the point in
> Perl?
>
> Thanks!
>
> Alex
>
<snip>
# -- round bar to the nearest integer
$bar = int( $bar + 0.5 );
# -- round bar to the nearest 0.01
$bar = int( $bar/0.01 + 0.5 ) * 0.01;
# -- round bar to the nearest 100
$bar = int( $bar/100 + 0.5 ) * 100;
get the picture ?
when you apply this to your example you may get
# -- filesize in kbytes
$filesize = (stat $filename)[7] / 1024
# -- round it to the nearest whole number
$filesize = int( $filesize + 0.5 );
--
Doug Perham o{..}o
dperham@wgate.com moo! (oo)___
WorldGate Communications, Inc. (______)\
/ \ / \
------------------------------
Date: 31 Jul 2000 20:57:58 GMT
From: Tina Mueller <tina@streetmail.com>
Subject: Re: Rounding numbers in Perl
Message-Id: <8m4p8m$5mcr3$4@ID-24002.news.cis.dfn.de>
hi,
Doug Perham <dperham@dperham.eng.tvol.net> wrote:
> "Alex T." <samara_biz@hotmail.com> writes:
>> FAQ basically tells me that I have to write my own truncating/rounding function.
>>
>> Did anyone have to deal with truncation to a certain digit after the point in
>> Perl?
> # -- round bar to the nearest integer
> $bar = int( $bar + 0.5 );
hm, that's what sprintf is for, like it was said
in another posting.
perldoc sprintf
tina
--
http://tinita.de \ enter__| |__the___ _ _ ___
tina's moviedatabase \ / _` / _ \/ _ \ '_(_-< of
search & add comments \ \ _,_\ __/\ __/_| /__/ perception
please no answers via email unless followup is set to poster.
------------------------------
Date: Mon, 31 Jul 2000 13:55:06 -0700
From: RS@ <rschram@reed.edu>
Subject: Re: Rounding numbers in Perl
Message-Id: <0d87be38.608e4ce0@usw-ex0102-014.remarq.com>
I also did not read the perl FAQ on printf. Does the following
algorithm look good?
Perl has a modulus operator (Camel 1996, p.82). Why not test the
actual value (a) for its highest integer divisor where remainder
is less than one?
Let a be an arbitrary positive real number,
Let n be the set of non-zero, positive integers.
for each n
a % n = q, R
if q=1 and 1 > R > 0,
then stop. n is the closest integer to a without going
over. Else next n.
end for.
And if you wanted to round up when R is greater than .5, you
could test
if R > 0.5, n' = n+1,
else n'=n.
So, for example if a = 100.987, n would hop along until it hit
n=100 (as 100.987 % 100 = 1, 0.987). You could claim that n' =
100, but n' = 101 is best, since R = 0.987 > 0.5.
To round to the nearest tenth decimal place, do the above,
letting R*10 = a. n'sub2/10 + n' would be the figure rounded to
the nearest tenth.
Also, an integer minus an integer is always an integer so :
foreach n
a - n = d
if 0 < d < 1
n' = n iff abs(d) < 0.5
else n' = n+1
endif
endfor
-----------------------------------------------------------
Got questions? Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com
------------------------------
Date: Mon, 31 Jul 2000 21:05:47 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Rounding numbers in Perl
Message-Id: <sobqhbcu9ft76@corp.supernews.com>
In article <8m4p8m$5mcr3$4@ID-24002.news.cis.dfn.de>,
Tina Mueller <news@tinita.de> wrote:
: Doug Perham <dperham@dperham.eng.tvol.net> wrote:
:
: > # -- round bar to the nearest integer
: > $bar = int( $bar + 0.5 );
:
: hm, that's what sprintf is for, like it was said
: in another posting.
Well, sprintf implements round-to-even. Truncation is usually
round-to-zero or (rarely) round-to-negative-infinity.
Greg
--
People who comprehend a thing to its very depths rarely stay faithful to it
forever. For they have brought its depths into the light of day: and in the
depths there is always much that is unpleasant to see.
-- Nietzsche
------------------------------
Date: 31 Jul 2000 16:19:06 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Rounding numbers in Perl
Message-Id: <8m4qga$85f$1@provolone.cs.utexas.edu>
In article <0d87be38.608e4ce0@usw-ex0102-014.remarq.com>,
RS@ <rschram@reed.edu> wrote:
>Perl has a modulus operator (Camel 1996, p.82). Why not test the
>actual value (a) for its highest integer divisor where remainder
>is less than one?
Call me silly, but if you're trying to round something, presumably you
have a value which isn't an integer, in which case I'm not clear on
just how it's going to have an integer divisor.
Anyway, it seems like the simplest method is to just use
multiplication, division, addition, and the int() function:
sub round
{
my ($number, $precision) = @_;
$number /= $precision;
$number = int ($number + 0.5);
$number *= $precision;
return $number;
}
Or, the more concise version:
sub round
{
my ($number, $precision) = @_;
return $precision * int ( ($number / $precision) + 0.5 );
}
Or, the too-concise version:
sub round { $_[1]*int($_[0]/$_[1]+.5); }
Note that I'm not claiming that the too-concise version is minimal.
- Logan
------------------------------
Date: Mon, 31 Jul 2000 16:05:23 -0400
From: Sean Lavelle <slavelle@concentus-tech.com>
Subject: SCP instead of FTP?
Message-Id: <3985DC02.6F122E8F@concentus-tech.com>
Hi,
I'm using Net::FTP to move files from a unix server to an NT
server. The one problem that I have though is that the password and
username gets sent cleartext (ftp's fault, not perl's). I was wondering
if there is a drop in repacement for ftp like scp that I could change
the protocol without having to rewrite the progam.
Sea
--
Sean Lavelle
Concentus Technology Corporation
5115 Parkcenter Ave, Suite 150
Dublin, Ohio 43017
Phone (614) 799-4317
slavelle@concentus-tech.com
------------------------------
Date: Mon, 31 Jul 2000 16:01:11 -0400
From: Margit Meyer <margit@us.ibm.com>
Subject: select/vec/pipes question
Message-Id: <3985DB07.B0EDA658@us.ibm.com>
We have a program which sends off child processes to issue commands or
run scripts/programs on other AIX systems. When the program executes
from the child process for example "lslpp -l" and then sleep 20
seconds, one line of output is displayed, then the sleep 20 seconds,
then the rest of the output. Nothing should be returned until the child
process has completed (after the sleep 20 seconds) . We think we have
narrowed down where the escapee appears and seems to be caused by a
select statement. Can someone help with this? The following is sample
code where this ocurrs:
#Loop while at least one file handle is open
while ($num_fh > 0) {
#Wait for outstanding I/O from stdout or stderr
$nready = select($rout = $rin, undef, undef, undef);
last if ($nready == 0);
#Data from stdout
if (vec($rout, fileno($ofh), 1)) {
$_ = <$ofh>;
if (length($_)) {
print STDOUT "$hostn: $_";
} else {#EOF has been reached
vec($rin,fileno($ofh),1)=0;
$num_fh-;
}
}
}
Any help would be greatly appreciated.
Thank you.
Margit Meyer
------------------------------
Date: Mon, 31 Jul 2000 18:29:02 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: simple regex?
Message-Id: <3985c56e.6686$353@news.op.net>
In article <8m49fc$30b$1@nnrp1.deja.com>, <pcj1@my-deja.com> wrote:
>I'm trying to write a regular expression which matches any text other
>than a literal '<|'. For example the input 'abc<def' should match
>completely, while 'abc<|def' should match 'abc'.
One common strategy looks like this:
(
(?:
(?!<\|).
)+
)
There is a problem with the idea you showed in your article:
(
(?:
[^<]
|
<[^|]
)+
)
This will match the entire string "abc<<|def" instead of stopping
after the first < as it ought.
The s/// approach mentioned in a parallel article is probably simplest.
------------------------------
Date: Mon, 31 Jul 2000 19:38:13 GMT
From: pcj1@my-deja.com
Subject: Re: simple regex?
Message-Id: <8m4kj5$cc9$1@nnrp1.deja.com>
> : [snip]
>
> How about a different approach?
>
> while (<>) {
> s/<\|.*$//; # delete everything after first <| in $_
> print;
> }
>
Unfortunately I really can't take a different approach. I'm actually
writing a lexer (not in perl) that uses regular expressions to identify
tokens. I'm using Perl to make sure I'm not crazy. So anyways I need
your basic DFA flavored regex if at all possible.
Paul
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 31 Jul 2000 20:44:04 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: simple regex?
Message-Id: <sobp8keu9ft115@corp.supernews.com>
In article <8m4kj5$cc9$1@nnrp1.deja.com>,
<pcj1@my-deja.com> wrote:
: > How about a different approach?
:
: Unfortunately I really can't take a different approach.
That would have been nice to know up front. :-/
: I'm actually
: writing a lexer (not in perl) that uses regular expressions to identify
: tokens. I'm using Perl to make sure I'm not crazy. So anyways I need
: your basic DFA flavored regex if at all possible.
How's this:
# from rereading your first post, the magic sequence
# seems to be <{ and not <|
while (<>) {
chomp;
if (/([^<]+(?:<(?:[^{]|$)|[^<])*)/) {
print "\$1 = '$1'\n";
}
else {
print "No match for '$_'\n";
}
}
I know the regular expression is ugly, but you said DFA. If it's the
case that you simply abused the term, then use mjd's regular expression
instead. Keep in mind that (?:...) in a Perl regular expression is for
grouping only (i.e., it doesn't produce a backreference).
Greg
--
If you believe in yourself and have dedication and pride and never quit,
you'll be a winner. The price of victory is high but so are the rewards.
-- Paul "Bear" Bryant
------------------------------
Date: Mon, 31 Jul 2000 18:40:18 GMT
From: emavres@my-deja.com
Subject: splitting on spaces
Message-Id: <8m4h6h$9ig$1@nnrp1.deja.com>
Can anyone show me how to split a line that is seperated by spaces but
not by the same amount of spaces?
Example:
"Hello how can I help you"
I would like to get each word.
Thanks
Mike
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 31 Jul 2000 18:43:24 GMT
From: ericr@yankthechain.com
Subject: Re: splitting on spaces
Message-Id: <8m4hca$9rp$1@nnrp1.deja.com>
In article <8m4h6h$9ig$1@nnrp1.deja.com>,
emavres@my-deja.com wrote:
> Can anyone show me how to split a line that is seperated by spaces but
> not by the same amount of spaces?
>
> Example:
>
> "Hello how can I help you"
>
> I would like to get each word.
>
> Thanks
> Mike
>
split(/ /, $string)); will do that no matter how many space are there.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 31 Jul 2000 13:55:28 -0500
From: Tom Briles <sariq@texas.net>
Subject: Re: splitting on spaces
Message-Id: <3985CBA0.73F974@texas.net>
ericr@yankthechain.com wrote:
>
> In article <8m4h6h$9ig$1@nnrp1.deja.com>,
> emavres@my-deja.com wrote:
> > Can anyone show me how to split a line that is seperated by spaces but
> > not by the same amount of spaces?
> >
> > Example:
> >
> > "Hello how can I help you"
> >
> > I would like to get each word.
>
> split(/ /, $string)); will do that no matter how many space are there.
Are you sure? Did you test it?
Perhaps you (and the OP) would like to see what the manpage for split()
has to say.
- Tom
------------------------------
Date: Mon, 31 Jul 2000 18:57:34 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: splitting on spaces
Message-Id: <sobj0ulg9ft180@corp.supernews.com>
In article <8m4h6h$9ig$1@nnrp1.deja.com>,
<emavres@my-deja.com> wrote:
: Can anyone show me how to split a line that is seperated by spaces but
: not by the same amount of spaces?
:
: Example:
:
: "Hello how can I help you"
:
: I would like to get each word.
What have you tried? What documentation have you read?
Greg
--
In the end, we had a visit from an embarrassed Digital Special Systems guy
who found an unused inverter pin and added some white wires. These days,
such a problem would be harder to fix in the field.
-- dmr
------------------------------
Date: Mon, 31 Jul 2000 21:28:40 GMT
From: Rodney Engdahl <red_orc@my-deja.com>
Subject: Re: splitting on spaces
Message-Id: <8m4r22$hjg$1@nnrp1.deja.com>
In article <8m4h6h$9ig$1@nnrp1.deja.com>,
emavres@my-deja.com wrote:
> Can anyone show me how to split a line that is seperated by spaces but
> not by the same amount of spaces?
>
> Example:
>
> "Hello how can I help you"
>
> I would like to get each word.
you might want to look into regexps to change consecutive spaces into a
single space, and then look into split to split on a space.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 31 Jul 2000 21:33:04 GMT
From: ericr@yankthechain.com
Subject: Re: splitting on spaces
Message-Id: <8m4rag$huc$1@nnrp1.deja.com>
In article <3985CBA0.73F974@texas.net>,
Tom Briles <sariq@texas.net> wrote:
> ericr@yankthechain.com wrote:
> >
> > In article <8m4h6h$9ig$1@nnrp1.deja.com>,
> > emavres@my-deja.com wrote:
> > > Can anyone show me how to split a line that is seperated by
spaces but
> > > not by the same amount of spaces?
> > >
> > > Example:
> > >
> > > "Hello how can I help you"
> > >
> > > I would like to get each word.
> >
> > split(/ /, $string)); will do that no matter how many space are
there.
>
> Are you sure? Did you test it?
>
> Perhaps you (and the OP) would like to see what the manpage for split
()
> has to say.
>
> - Tom
?
This works:
@myarray = split(/ /, $string);
There was a typo with an extra ")", but come on, no one's perfect.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 31 Jul 2000 21:49:02 GMT
From: Tina Mueller <tina@streetmail.com>
Subject: Re: splitting on spaces
Message-Id: <8m4s8e$5mcr3$8@ID-24002.news.cis.dfn.de>
hi,
ericr@yankthechain.com wrote:
> In article <3985CBA0.73F974@texas.net>,
> Tom Briles <sariq@texas.net> wrote:
>> ericr@yankthechain.com wrote:
>> >
>> > In article <8m4h6h$9ig$1@nnrp1.deja.com>,
>> > emavres@my-deja.com wrote:
>> > > Can anyone show me how to split a line that is seperated by
> spaces but
>> > > not by the same amount of spaces?
>> > >
>> > > Example:
>> > >
>> > > "Hello how can I help you"
>> > >
>> > > I would like to get each word.
>> >
>> > split(/ /, $string)); will do that no matter how many space are
> there.
>>
>> Are you sure? Did you test it?
>>
> This works:
> @myarray = split(/ /, $string);
it does?
I get 16 elements in @myarray (that's not wat the
poster wants, I guess).
perhaps you meant
my @myarray = split(' ', $string);
look at the documentation:
(like it was said before)
perldoc -f split
tina
--
http://tinita.de \ enter__| |__the___ _ _ ___
tina's moviedatabase \ / _` / _ \/ _ \ '_(_-< of
search & add comments \ \ _,_\ __/\ __/_| /__/ perception
please no answers via email unless followup is set to poster.
------------------------------
Date: Mon, 31 Jul 2000 19:57:15 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: string -> array?
Message-Id: <sfmboskpmof99js60uuettb4g6uhgbmvpo@4ax.com>
Simon H. wrote:
>I need to convert a string, $word, and also a binary number, $bivect, to
>an array of characters so I can push and pop at leisure, and then
>convert the string back again. Any suggestions on how to do this?
Get rid of your C approach, and do it in a Perl way. For example, using
substr() or regexes.
--
Bart.
------------------------------
Date: Mon, 31 Jul 2000 12:52:02 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: string -> array?
Message-Id: <MPG.13ef78b84750d12e98ac11@nntp.hpl.hp.com>
In article <slrn8ob2nn.7k6.marcel@gandalf.local> on Mon, 31 Jul 2000
14:14:01 GMT, Marcel Grunauer <marcel@codewerk.com> says...
> On Mon, 31 Jul 2000 15:09:58 +0100, Simon H. <srh104@york.ac.uk> wrote:
>
> >I need to convert a string, $word, and also a binary number, $bivect, to
> >an array of characters so I can push and pop at leisure, and then
> >convert the string back again. Any suggestions on how to do this?
>
>
> For example, using split() and join():
>
>
> for my $s (qw/110001010 hello/) {
> my @a = split //, $s;
> pop @a;
> push @a, 4..6;
> my $r = join '' => @a;
> print "$r\n";
> }
>
> perldoc -f split
> perldoc -f join
Sure, but the use of substr() on the string instead of splitting it and
joining it is likely to be much more efficient.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 3862
**************************************