[31798] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3061 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 4 11:09:31 2010

Date: Wed, 4 Aug 2010 08:09:12 -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           Wed, 4 Aug 2010     Volume: 11 Number: 3061

Today's topics:
    Re: Extract variable length numbers (tab delimitered) f <thomas@tifozi.net>
    Re: Extract variable length numbers (tab delimitered) f <wolf@gsheep.com>
    Re: Help with regular expression <tzz@lifelogs.com>
        I think an assertion might do this? <misterperl@gmail.com>
    Re: I think an assertion might do this? <NoSpamPleaseButThisIsValid3@gmx.net>
    Re: I think an assertion might do this? <tadmc@seesig.invalid>
    Re: I think an assertion might do this? <justin.1007@purestblue.com>
    Re: I think an assertion might do this? <uri@StemSystems.com>
        looking for Hamming+BCH+Reed - Solomon Codes in perl <sting.t2@gmail.com>
    Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <justin.1007@purestblue.com>
    Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <willem@turtle.stack.nl>
    Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <ralph@happydays.com>
    Re: Spreadsheet shows scientific notation occasionally  <sherm.pendley@gmail.com>
        Spreadsheet shows scientific notation occasionally with <needpassion@gmail.com>
    Re: Spreadsheet shows scientific notation occasionally  <hjp-usenet2@hjp.at>
        Storing array elements tab delimitered in a string <thomas@tifozi.net>
    Re: Storing array elements tab delimitered in a string <ben@morrow.me.uk>
    Re: Storing array elements tab delimitered in a string <sherm.pendley@gmail.com>
    Re: Storing array elements tab delimitered in a string <RedGrittyBrick@spamweary.invalid>
    Re: Storing array elements tab delimitered in a string <thomas@tifozi.net>
    Re: Storing array elements tab delimitered in a string <bugbear@trim_papermule.co.uk_trim>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 4 Aug 2010 04:50:19 +0200
From: "Thomas Andersson" <thomas@tifozi.net>
Subject: Re: Extract variable length numbers (tab delimitered) from a string?
Message-Id: <8bs2qrFvckU1@mid.individual.net>

Justin C wrote:
> On 2010-08-03, Thomas Andersson <thomas@tifozi.net> wrote:
>> As the topic says. I ahve a settings file where each line contains 2
>> numbers of varying length and I want to extract each number and
>> assign to a variable, how would I go about that?
>
> TMTOWTDI, here's one, it may not be a good one.

> How long are you numbers? Are they formatted?

2 variable length numbers that are tab delimiterd, someone suggest the split
function which worked perfect.

my ($cpid, $lproc) = split (/\t/, $pidlist); 




------------------------------

Date: Wed, 04 Aug 2010 12:44:19 +0200
From: wolf <wolf@gsheep.com>
Subject: Re: Extract variable length numbers (tab delimitered) from a string?
Message-Id: <i3bgaa$bf3$02$1@news.t-online.com>

sln@netherlands.com schrieb:
> On Tue, 03 Aug 2010 15:10:47 +0200, wolf <wolf@gsheep.com> wrote:
> 
>> Thomas Andersson schrieb:
>>> As the topic says. I ahve a settings file where each line contains 2 numbers 
>>> of varying length and I want to extract each number and assign to a 
>>> variable, how would I go about that? 
>>>
>>>
>> use SPLIT:  split( /\s+/, $input) splits on any whitespace(s) including 
>> tab(s). split( /\t/, $input) splits on every tab.
>>
>> open (my $infile, '<', 'mynumbers.txt') or die;
>> my ($input, $number1, $number2);
>>
>> while ($input = <$infile>) {
>> 	chomp $input;
>> 	($number1, $number2) = split( /\s+/, $input);
> 
> Don't forget to validate $number(s) or you could run
> into errors when doing stuff like 
>      if $number1 == $number2
> 
> So after the split() it could be validated something like
>      $number1 =~ s/^\s+//;
>      $number1 =~ s/\s+$//;
>      if $number1 =~ /^[+-]?\d*?\.?\d+$/  # for non-exponent
> 
> Or it can all be done in one line
> 	($number1, $number2) = $input =~ /\s*([+-]?\d*?\.?\d+)\s+([+-]?\d*?\.?\d+)/;
> 
> -sln

Dear gents,

these are all valid arguments to improving the code :)

However, since the original poster didn't even know how to wield SPLIT, 
i wanted to keep it as simple and un-confusing as possible, 
demonstrating just SPLIT as the important thing to handle.
As such the code works well enough :p

Cheers, wolf


------------------------------

Date: Wed, 04 Aug 2010 08:14:10 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Help with regular expression
Message-Id: <87fwyubjvh.fsf@lifelogs.com>

On Tue, 3 Aug 2010 22:11:16 +0200 Helmut Richter <hhr-m@web.de> wrote: 

HR> On Tue, 3 Aug 2010, Ted Zlatanov wrote:
>> I didn't parse the requirements that way, but that's probably my error.

HR> Well, when I set up the grammar, there were ambiguities of interpretation 
HR> of the requirements. This is just my interpretation. But at least I chose 
HR> it because I found it to be the most plausible, and not in order that 
HR> parsing be possible.

If you and the OP are happy with your interpretation, that's all there
is to it.  There's no universal truth with requirements; it's always
more productive to understand *why* the requirements are established.

In this case, all the nested parens with confusing (to me) parsing rules
suggest a broken attempt at writing a DSL rather than an interesting
problem.

Ted


------------------------------

Date: Wed, 4 Aug 2010 06:02:44 -0700 (PDT)
From: Mr P <misterperl@gmail.com>
Subject: I think an assertion might do this?
Message-Id: <4d6500db-c586-46d3-bd83-51301a319442@q22g2000yqm.googlegroups.com>

Hey Perlistas...

I wanted to replace any and all multiple sequential EOLs with a single
one. This worked nicely:

(a)  s/(\n)\n+/$1/g;

But thinking abot it some more. it occurred to me that these wouldn't
quite work

(b) s/(\n)\n/$1/g;
(c) s/\n(\n)/$1/g;
(d) s/\n\n/\n/g;

because even though they are greedy, they don't retrace. And
predictably they didn't work.

I've never really used assertions, at least not enough to be familiar
with them. I guess this would be a positive look-behind to force the
engine to retrace?

Can someone offer an example please where I can use something like s/
(\n)\n/$1/g; with an assertion to do what (a) does? It would be
instructional for me to see this example.

Thanks


------------------------------

Date: Wed, 04 Aug 2010 15:40:46 +0200
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: I think an assertion might do this?
Message-Id: <4c596ddf$0$6979$9b4e6d93@newsspool4.arcor-online.net>

On 04.08.2010 15:02, Mr P wrote:
> Hey Perlistas...
> 
> I wanted to replace any and all multiple sequential EOLs with a single
> one. This worked nicely:
> 
> (a)  s/(\n)\n+/$1/g;
> 
> Can someone offer an example please where I can use something like s/
> (\n)\n/$1/g; with an assertion to do what (a) does? It would be
> instructional for me to see this example.

Are you looking for this?
s/(\n)(?=\n)//g; # global remove \n if another \n follows

Other than that, sometimes you also see something like this:
1 while s#\n\n#\n#;

Wolf


------------------------------

Date: Wed, 04 Aug 2010 08:59:03 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: I think an assertion might do this?
Message-Id: <slrni5iscm.6pa.tadmc@tadbox.sbcglobal.net>

Mr P <misterperl@gmail.com> wrote:

> I wanted to replace any and all multiple sequential EOLs with a single
> one. This worked nicely:
>
> (a)  s/(\n)\n+/$1/g;


You don't need regular expressions to do that:

    tr/\n/\n/s;
or
    tr/\n//s;


> But thinking abot it some more. it occurred to me that these wouldn't
> quite work
>
> (b) s/(\n)\n/$1/g;
> (c) s/\n(\n)/$1/g;
> (d) s/\n\n/\n/g;
>
> because even though they are greedy, 


Greediness applies only when you have quantifiers.

There is no "greedy" involved with any of those, as none
of them have quantifiers.

So greed is not why they are not working...


> they don't retrace. 


What does "retrace" mean when you say it?


> I've never really used assertions, at least not enough to be familiar
> with them. I guess this would be a positive look-behind to force the
> engine to retrace?


I'm guessing that "retrace" means "do the search over again starting
from the beginning" or some such?

In other words, reset the match pos()ition?

    pos=0 while s/\n\n/\n/g;

But since starting a match over again resets the match position
to zero anyway, just this will also do it:

    1 while s/\n\n/\n/g;
    

> Can someone offer an example please where I can use something like s/
> (\n)\n/$1/g; with an assertion to do what (a) does?


I don't see how assertions can help with this problem...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Wed, 4 Aug 2010 14:38:47 +0100
From: Justin C <justin.1007@purestblue.com>
Subject: Re: I think an assertion might do this?
Message-Id: <747oi7-pp9.ln1@zem.masonsmusic.co.uk>

On 2010-08-04, Mr P <misterperl@gmail.com> wrote:
> Hey Perlistas...
>
> I wanted to replace any and all multiple sequential EOLs with a single
> one. This worked nicely:
>
> (a)  s/(\n)\n+/$1/g;
>
> But thinking abot it some more. it occurred to me that these wouldn't
> quite work
>
> (b) s/(\n)\n/$1/g;
> (c) s/\n(\n)/$1/g;
> (d) s/\n\n/\n/g;
>
> because even though they are greedy, they don't retrace. And
> predictably they didn't work.
>
> I've never really used assertions, at least not enough to be familiar
> with them. I guess this would be a positive look-behind to force the
> engine to retrace?
>
> Can someone offer an example please where I can use something like s/
> (\n)\n/$1/g; with an assertion to do what (a) does? It would be
> instructional for me to see this example.

I would use s/(\n){2,}/\n/g  that's \n two or more times.

   Justin.

-- 
Justin C, by the sea.


------------------------------

Date: Wed, 04 Aug 2010 10:12:59 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: I think an assertion might do this?
Message-Id: <8762zqo49g.fsf@quad.sysarch.com>

>>>>> "JC" == Justin C <justin.1007@purestblue.com> writes:

  JC> I would use s/(\n){2,}/\n/g  that's \n two or more times.

no need for the () there as you don't use the grab. and tad's tr/// is
the best solution anyhow.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Wed, 4 Aug 2010 04:52:19 -0700 (PDT)
From: Amir <sting.t2@gmail.com>
Subject: looking for Hamming+BCH+Reed - Solomon Codes in perl
Message-Id: <dc815c7b-b3ee-41b6-92d6-51efce6bb66a@f42g2000yqn.googlegroups.com>

Hi,
do you have any idea where can I find implementations of the Hamming
+BCH+Reed - Solomon Codes in Perl?

Thanks in advance
 -Amir


------------------------------

Date: Wed, 4 Aug 2010 14:24:46 +0100
From: Justin C <justin.1007@purestblue.com>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <u96oi7-pp9.ln1@zem.masonsmusic.co.uk>

On 2010-08-03, Ralph Malph <ralph@happydays.com> wrote:
> tl, dnr

Tedious life-form, do not resuscitate?

   Justin.

-- 
Justin C, by the sea.


------------------------------

Date: Wed, 4 Aug 2010 14:15:15 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <slrni5itfj.lfb.willem@turtle.stack.nl>

Justin C wrote:
) On 2010-08-03, Ralph Malph <ralph@happydays.com> wrote:
)> tl, dnr
)
) Tedious life-form, do not resuscitate?

More vulgarly: Trolling loser, do not reply.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Wed, 04 Aug 2010 10:17:51 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <54491$4c59768f$40779ac3$6344@news.eurofeeds.com>

On 8/4/2010 9:24 AM, Justin C wrote:
> On 2010-08-03, Ralph Malph<ralph@happydays.com>  wrote:
>> tl, dnr
too long, did not read


------------------------------

Date: Tue, 03 Aug 2010 22:18:33 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Spreadsheet shows scientific notation occasionally with some cells,  but it is wrong
Message-Id: <m262zrm87a.fsf@sherm.shermpendley.com>

mike yue <needpassion@gmail.com> writes:

> I am not sure if this is related to the perl script(uses WriteExcel
> module), or related to my MS office excel settings.
>
> e.g. Here are six cells showing in a spreadsheet:
>
> 10b1e4	       146914
> 10b818	       14db80
> 8.054E+33	808c940
>
> The "8.054E+33" is actually from a string "8054e30" representing a
> memory address, which is definitely not 8.054E+33.
> But all other cells are show correct - that is weird.
>
> My perl script uses WriteExcel, and the cells are with format which
> only set_align('right').
>
> Anyone got ideas?

Have you tried prefixing the values in that column with "0x"? If Excel
is determined to interpret them as numbers, that may at least convince
it that they're hexadecimal numbers.

sherm--

-- 
Sherm Pendley                <www.shermpendley.com>
                             <www.camelbones.org>
Cocoa Developer


------------------------------

Date: Tue, 3 Aug 2010 16:47:58 -0700 (PDT)
From: mike yue <needpassion@gmail.com>
Subject: Spreadsheet shows scientific notation occasionally with some cells,  but it is wrong
Message-Id: <54d84c8b-685c-40e4-ab66-5e79224c9428@i24g2000yqa.googlegroups.com>

I am not sure if this is related to the perl script(uses WriteExcel
module), or related to my MS office excel settings.

e.g. Here are six cells showing in a spreadsheet:

10b1e4	       146914
10b818	       14db80
8.054E+33	808c940

The "8.054E+33" is actually from a string "8054e30" representing a
memory address, which is definitely not 8.054E+33.
But all other cells are show correct - that is weird.

My perl script uses WriteExcel, and the cells are with format which
only set_align('right').

Anyone got ideas?

Thank you guys for your attention.


------------------------------

Date: Wed, 4 Aug 2010 10:15:25 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Spreadsheet shows scientific notation occasionally with some cells,  but it is wrong
Message-Id: <slrni5i8ct.lh5.hjp-usenet2@hrunkner.hjp.at>

On 2010-08-03 23:47, mike yue <needpassion@gmail.com> wrote:
> I am not sure if this is related to the perl script(uses WriteExcel
> module), or related to my MS office excel settings.
>
> e.g. Here are six cells showing in a spreadsheet:
>
> 10b1e4	       146914
> 10b818	       14db80
> 8.054E+33	808c940
>
> The "8.054E+33" is actually from a string "8054e30" representing a
> memory address, which is definitely not 8.054E+33.

% perl -le 'print 8054e30'
8.054e+33


> But all other cells are show correct - that is weird.
>
> My perl script uses WriteExcel, and the cells are with format which
> only set_align('right').

Which method do you use to write the cell? "write" or "write_string"?

If you use write, it needs to guess whether the thing you want to write
is a string or a number (a perl scalar can be both), and since 8054e30
looks like a number it guesses that it is one. Use write_string if you
want to write a string. If you are already using write_string, it's
probably a bug.

	hp



------------------------------

Date: Wed, 4 Aug 2010 04:53:14 +0200
From: "Thomas Andersson" <thomas@tifozi.net>
Subject: Storing array elements tab delimitered in a string
Message-Id: <8bs30aFvulU1@mid.individual.net>

I there an easy way to stora all elements of an array tab delimitered in a 
string (need to prepare collected data for import to a database).? 




------------------------------

Date: Wed, 4 Aug 2010 04:10:42 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Storing array elements tab delimitered in a string
Message-Id: <ia2ni7-52s.ln1@osiris.mauzo.dyndns.org>


Quoth "Thomas Andersson" <thomas@tifozi.net>:
> I there an easy way to stora all elements of an array tab delimitered in a 
> string (need to prepare collected data for import to a database).? 

perldoc -f join

If your elements might contain tabs, you are working with a variant of CSV. 
Use Text::CSV_XS.

Ben



------------------------------

Date: Tue, 03 Aug 2010 23:18:19 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Storing array elements tab delimitered in a string
Message-Id: <m2pqxzvzes.fsf@sherm.shermpendley.com>

"Thomas Andersson" <thomas@tifozi.net> writes:

> I there an easy way to stora all elements of an array tab delimitered in a 
> string (need to prepare collected data for import to a database).? 

join() is the opposite of split().

  my $record = join("\t", @fields);

However! Note that this simple approach is fragile - if any of your
fields contain tab characters, it will break. You might want to use
map() to quote each field before joining them together:

  my $record = join("\t", map("\"$_\"", @fields))

That can break too, if your fields can contain quotes *and* tabs. There
comes a point where it's easier to use DBI to connect to your database
and insert your data directly. :-)

sherm--

-- 
Sherm Pendley                <www.shermpendley.com>
                             <www.camelbones.org>
Cocoa Developer


------------------------------

Date: Wed, 04 Aug 2010 10:41:32 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Storing array elements tab delimitered in a string
Message-Id: <4c5935ce$0$2535$da0feed9@news.zen.co.uk>

On 04/08/2010 04:18, Sherm Pendley wrote:
> "Thomas Andersson"<thomas@tifozi.net>  writes:
>
>> I there an easy way to stora all elements of an array tab delimitered in a
>> string (need to prepare collected data for import to a database).?
>
>    my $record = join("\t", @fields);
>
> However! Note that this simple approach is fragile - if any of your
> fields contain tab characters, it will break. You might want to use
> map() to quote each field before joining them together:
>
>    my $record = join("\t", map("\"$_\"", @fields))
>
> That can break too, if your fields can contain quotes *and* tabs. There
> comes a point where it's easier to use DBI to connect to your database
> and insert your data directly. :-)

Since database tools for loading data often allow you to specify a 
separator character, I sometimes find it easier to find a character that 
isn't present in the data. I haven't been so adventurous as to use the 
ASCII field-separator character (FS) but often use a vertical bar (|).


Just my ยค0.02 worth
-- 
RGB


------------------------------

Date: Wed, 4 Aug 2010 15:33:07 +0200
From: "Thomas Andersson" <thomas@tifozi.net>
Subject: Re: Storing array elements tab delimitered in a string
Message-Id: <8bt8geFg1lU1@mid.individual.net>

Sherm Pendley wrote:

>> I there an easy way to stora all elements of an array tab
>> delimitered in a string (need to prepare collected data for import
>> to a database).?
>
> join() is the opposite of split().
>
>  my $record = join("\t", @fields);
>
> However! Note that this simple approach is fragile - if any of your
> fields contain tab characters, it will break. You might want to use
> map() to quote each field before joining them together:
>
>  my $record = join("\t", map("\"$_\"", @fields))
>
> That can break too, if your fields can contain quotes *and* tabs.
> There comes a point where it's easier to use DBI to connect to your
> database and insert your data directly. :-)

Thank you, my array will contain single words or numbers only so shouldn't 
be a problem. Might colons be a problem? (I know my script fails at 
collecting the fields containing a time ref and I assume it's due to the 
colon).

Best Wishes
Thomas 




------------------------------

Date: Wed, 04 Aug 2010 14:34:23 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: Storing array elements tab delimitered in a string
Message-Id: <--adnWMkt__C8cTRnZ2dnUVZ8nEAAAAA@brightview.co.uk>

Thomas Andersson wrote:
> I there an easy way to stora all elements of an array tab delimitered in a 
> string (need to prepare collected data for import to a database).? 

What import formats does your database support?

   BugBear


------------------------------

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 3061
***************************************


home help back first fref pref prev next nref lref last post