Adding a delimiter inbetween number characters and letter characters

M

Martin Kissner

Michele Dondi wrote :
There's hardly any need IMHO to include line numbers. To be fair,
personally I find them to be disturbing...

I forgot to "set :nonu" before copying.
Sorry for the inconvenience.
Do you _really_ need this $lastline algorithmic madness?

Good question. What do you think of that solution?

#!/usr/bin/perl

use warnings;
use strict;

local $/;
my $content = <DATA>;
$content =~ s/\n\D//g;
$content =~ s/(^\d+|\n\d)/$1-/g;
print $content;

__DATA__
1sampletextsampletextsamplet22tsampletextsampletextsampletext
2sampletextsampletextsampletextsam56etextsampletextsampletext
3sampletextsampletextsampletextsampletextsampletextsampletext
[...]
There's no need to use the &-form of sub call in Perl5: most likely it
won't do what you mean. See 'perldoc perlsub'.
Just to make sure I understood:
Is the foo() form cheaper because it doesn't pass @_ ?

Thank you, for your feedback.
 
A

A. Sinan Unur

Michele Dondi wrote :
Just to make sure I understood:
Is the foo() form cheaper because it doesn't pass @_ ?

I am curious, did you actually bother to read 'perldoc perlsub'?

... If a subroutine is called using the "&" form, the argument list
is optional, and if omitted, no @_ array is set up for the subroutine:
the @_ array at the time of the call is visible to subroutine instead.
This is an efficiency mechanism that new users may wish to avoid.

If you use the foo() form, you are not passing any arguments at all. That is
allright if the sub does not take any arguments, but would be a problem if
the sub needed arguments to do its job. Please read (or re-read) perldoc
perlsub in its entirety to understand why the &foo is useful only in very few
specific instances.

Sinan
 
M

Martin Kissner

A. Sinan Unur wrote :
I am curious, did you actually bother to read 'perldoc perlsub'?

I didn't read all of it; it has more than 1300 lines.
But I tried to find, what Michele wanted to point me at.
English is not my native language.
Therefore reading (and understanding!) the docs is difficult for me and
takes a lot of time.
... If a subroutine is called using the "&" form, the argument list
is optional, and if omitted, no @_ array is set up for the subroutine:
the @_ array at the time of the call is visible to subroutine instead.
This is an efficiency mechanism that new users may wish to avoid.

I did read that, but i didn't really understand why it should be
necessary to avoid this.
[...] . Please read (or re-read) perldoc
perlsub in its entirety to understand why the &foo is useful only in very few
specific instances.

Well, I really tried, but things didn't get much clearer.
As far as I understand "new-style calls" enable subroutines which can
behave like built-in functions (which is beyond my scope for now).
Is that what you mean?
I'd appreciate if you could point me to a particular section of perlsub
to help me understand.

Thank you for your help.
Martin
 
A

A. Sinan Unur

A. Sinan Unur wrote :
I didn't read all of it; it has more than 1300 lines.

Well, you should read all of it as many times as necessary.
But I tried to find, what Michele wanted to point me at.
English is not my native language.

Nor is it mine.
Therefore reading (and understanding!) the docs is difficult for me
and takes a lot of time.

You should spend as much time as needed.
I did read that, but i didn't really understand why it should be
necessary to avoid this.

Just a simple example:

#! perl

use strict;
use warnings;

sub mysub {
join ':', @_;
}

print mysub 'Drive', 'Directory', "\n";
print &mysub 'Drive', 'Directory', "\n";

I am not particularly inclined to spend a lot of time with this at this
point. Unless you _know_ that you need & for a specific reason, you are
better off not using it. Simple rule, really.
[...] . Please read (or re-read) perldoc
perlsub in its entirety to understand why the &foo is useful only in
very few specific instances.
Well, I really tried, but things didn't get much clearer.
As far as I understand "new-style calls" enable subroutines which can
behave like built-in functions (which is beyond my scope for now).

AFAIK, that is made possible with the use of prototypes.
I'd appreciate if you could point me to a particular section of
perlsub to help me understand.

But I did. Are you so lazy that you did not bother to read the few lines
below the specific paragraph I quoted.

Sinan.
 
M

Martin Kissner

A. Sinan Unur wrote :
[...] Unless you _know_ that you need & for a specific reason, you are
better off not using it. Simple rule, really.

I will keep that in mind.
My Perl book (which is not the best one) mentioned only the &foo call.
But I did. Are you so lazy that you did not bother to read the few lines
below the specific paragraph I quoted.
Not at all.
I have realized that there is a difference between calling &foo, foo,
foo() and so on.
The example you postetd above (Thanks for that) gave me an idea of what
might go wrong when using &foo;
The rest might become clearer as I walk on learning.

Thanks again
Martin
 
A

Arndt Jonasson

Michele Dondi said:
[...]
So there are two possibilities: (i) you don't know Perl, and you don't
want to, and claim so; so you're asking una tantum: so far so fine!

Thank you for letting me learn an expression I hadn't encountered before.
I found it explained on
http://www.eurofound.eu.int/emire/ITALY/UNATANTUM-IT.html.
(I wonder what its etymology is - "una" is feminine, and "tantum" looks
like Latin neuter or masculine accusative.)
 
M

Michele Dondi

5 my $lastline="";
6 my @lastline;
[snip code]
Do you _really_ need this $lastline algorithmic madness?

Good question. What do you think of that solution?
^^^^
^^^^

Of _that_ solution or...
#!/usr/bin/perl

use warnings;
use strict;

local $/;
my $content = <DATA>;
$content =~ s/\n\D//g;
$content =~ s/(^\d+|\n\d)/$1-/g;
print $content;

__DATA__

....of _this_ solution?

Whatever, IIRC the OP just wanted to put in a delimiter between a
number of an arbitrary length at the beginning of each line and
whatever follows. Then I'd just do:

perl -pi -e 's/^(\d+)/$1-/' file1 file2...


Michele
 
M

Michele Dondi

[...] Unless you _know_ that you need & for a specific reason, you are
better off not using it. Simple rule, really.

I will keep that in mind.
My Perl book (which is not the best one) mentioned only the &foo call.

Probably it may have been a good one when it was written, but chances
are that in the meantime it became too old... (wild guess: it may well
be that it's never been "the best one", whatever this may mean, in the
first place!)


Michele
 
M

Martin Kissner

Michele Dondi wrote :
Whatever, IIRC the OP just wanted to put in a delimiter between a
number of an arbitrary length at the beginning of each line and
whatever follows. Then I'd just do:

perl -pi -e 's/^(\d+)/$1-/' file1 file2...

He also wants to join lines with no numbers at the beginning with the
preceding line.
 
M

Michele Dondi

Thank you for letting me learn an expression I hadn't encountered before.
I found it explained on
http://www.eurofound.eu.int/emire/ITALY/UNATANTUM-IT.html.
(I wonder what its etymology is - "una" is feminine, and "tantum" looks
like Latin neuter or masculine accusative.)

"Una" is an adverb and it means (either "in the same place" or) "at
the same time".

"Tantum" is an adverb as well and it means (either "so much" or)
"only".


Michele
 
M

Michele Dondi

10 PRINT "Having BASIC flashbacks, Michele? :)"
20 GOTO 10

Nice one!

But no, I really didn't. Actually I had an encounter with BASIC, but
it was much later than one could expect: at that time I had already
had some programming experience with Pascal and C. Maybe it was even
more traumatic then!!

It was in my first university year, and we had to take this
"programming lab"... in GWBASIC (IIRC) under 8088's!!!!

I remember the first lesson:

- Now I'll teach you how to turn on PCs!

- But we know how to do it, you just press a button!

- No guys, 8088's do _not_ have a swithc on button...

[...]

- Can we have a copy of the interpreter so to use it at home?

- No guys, it's not sw, it's hw: it's on the motherboard!

(Incidentally there was actually a sw version that I personally used
to a great advantage for I could at least type the program with a real
editor - I remember I used qedit under DOS at that time...)


Michele
 
M

Michele Dondi

He also wants to join lines with no numbers at the beginning with the
preceding line.

Oh, sorry, I hadn't noticed/didn't remember: you have my apologies.
Well, without even looking at your code, here's what I'd do:


#!/usr/bin/perl -l

use strict;
use warnings;

my $line='';

while (<>) {
chomp;
if (s/^\d+/$&-/) {
print $line;
$line=$_;
} else {
$line .= $_;
}
}

print $line;

__END__


Still _if_ I understood the problem correctly, now.


Michele
 
M

Martin Kissner

Michele Dondi wrote :
Oh, sorry, I hadn't noticed/didn't remember: you have my apologies.

There's no need for that ;)
So this part of your code

---snip---
if (s/^\d+/$&-/) {
print $line;
$line=$_;
} else {
$line .= $_;
}
---snap---

avoids th "lastline madness" which you rightfully criticized in my first
attempt.
Thanks for that clue.

In my second attempt - I repeat it here for your convenience - I tried
a diffrent approach.

[...]
local $/;
my $content = <DATA>;
$content =~ s/\n\D//g;
$content =~ s/(^\d+|\n\d)/$1-/g;
print $content;
[...]

Now I wonder whether there might be a performance penalty when
processing very large files.
I would guess yes; Is that right?
If there is another point of criticism, please let me know.

Martin
 
M

Martin Kissner

Michele Dondi wrote :
Apart from this I only can add that instead of $content I'd use $_, so
that I could save some keystrokes a la:

s/\n\D//g;
s/(^\d+|\n\d)/$1-/g;
print;

Thanks for that hint.
I use $_ far to rarely.
Also, the first regex removes the non-digit charachter:

Shame on me.
I should have used less confusing exampledata
s/\n(?=\D)//g;

I din't know (?=...) .
After consulting the docs its clear what this does.
I had used s/\n(\D)/$1/g
[...] And the second one has an error in it (a typo?), but even
fixing that, I'd do

s/^(\d+)/$1-/gm;

I first had trouble with this substitution, because I startet with
searching for the pattern \n\d, but this misses the first line.
your pattern, of course, is much more elegant.

Thanks for your advice.
This helped a lot.

Martin
 
M

Michele Dondi

Thanks for that hint.
I use $_ far to rarely.

Well, there's nothing wrong in, say,

$var =~ s/foo//;

but it tends to become tiresome if it has also to be

$var =~ s/foo//;
$var =~ s/bar//;
$var =~ s/baz//;

(not that it's strictly _wrong_ either), so you will find out that
people often do

for ($var) {
s/foo//;
s/bar//;
s/baz//;
}

instead, using C<for> "only" for its aliasing properties, or even

s/foo//, s/bar//, s/baz// for $var;

(depending on how complex the code acting on $_ really is). IIRC all
this is mentioned in a FAQ entry too.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top