Turning a list of scalars into an array?

R

robic0

Anno said:
You're using the wrong tool.

Why would split() be wrong?
The rule (known as Randal's rule) is "If you know what to throw away,
use split. If you know what to keep, use a capturing regex."

I agree that's a good rule of thumb, but...
In your case, what you want to keep it the digits at the end of each line.

And what the OP wants to throw away is everything but those digits.
You can get that by throwing away blanks and then accessing the n-th word
(4-th in the first case, 8-th in the second), but that's really roundabout.
Assuming a line in $_,

my ( $num) = /(\d+)$/;

will extract the number in both cases (untested).

my $num = (split)[-1];

Roundabout? To me it appears to be just as simple to figure out and
type. And isn't it actually faster to use the optimized split() rather
than invoking the regex engine in the conventional way?
What is the conventional way? Question of weather re.dll is opened at compile time,
depends on the complexity and if the simple built in parser can handle it or not.
The unknowing OP wants, as it appears to you, to grab an totally meaningless capture
of a, huh (?), *number* at the end of each line.

As totally meaningless the thought of grabbing a single number from lines from
a data file, and making an array out of it, you will extract the last item produced by
the split "my $num = (split)[-1];" (is that what its doing?).

That results in a unqualified, meaningless monolithic result not referencable anyway.
Hey, but maybe thats why "split" is good for you..
 
M

Matt Garrish

Mark Clements said:
No, not really. Is misspelling "Swedish" a joke that I just don't get? Too
sophisticated for me...

Don't waste the effort. He's been asked to use a spellchecker in the past,
but I'm convinced he wouldn't know the right word to use if he did. He's
Hooked on Phonics hidden failure...

Matt
 
R

robic0

No, not really. Is misspelling "Swedish" a joke that I just don't get?
Too sophisticated for me...

Mark

Well, hey Mark, maybe you could post in yuurr own words what *English* is
so I could cornform to "comprehensible English".

Aye's sure the bayuu fokes in Nworleens would like to hear it.

Stay clear of the classics like the works of Mark Twain, you wouldn't understand it!!

-robic0-
 
K

kwoody

robic0 said:
What is the conventional way? Question of weather re.dll is opened at >compile time, depends on the complexity and if the simple built in parser can >handle it or not.

DLL? I'm not running under Windows if thats what you are thinking.
The unknowing OP wants, as it appears to you, to grab an totally >meaningless capture of a, huh (?), *number* at the end of each line.

Yes I want the number at the end of the two lines in a flat text file,
the output of a simple cronjob.

The numbers might be meaningless to you, but the numbers are, in order,
the total number of emails in the mail queue and the total number of
unprocessed mails in the queue.
As totally meaningless the thought of grabbing a single number from lines >from a data file, and making an array out of it, you will extract the last item >produced by the split "my $num = (split)[-1];" (is that what its doing?).

I had my terms mixed up a bit so sue me. But the my $num = (split) [-1]
works like I wanted.
 
G

Gunnar Hjalmarsson

Mark said:
He didn't ask for Swedish: he asked for comprehensible English. Did you
not understand that?

Fast svenska skulle å andra sidan vara helt okej, tycker jag. ;-)
 
R

robic0

Don't waste the effort. He's been asked to use a spellchecker in the past,
but I'm convinced he wouldn't know the right word to use if he did. He's
Hooked on Phonics hidden failure...
I put ur post in the "English Checker" program and this is what it churrned out:

Change profession to "English teacher", is a distinct possibility!
 
R

robic0

robic0 said:
What is the conventional way? Question of weather re.dll is opened at >compile time, depends on the complexity and if the simple built in parser can >handle it or not.

DLL? I'm not running under Windows if thats what you are thinking.
The unknowing OP wants, as it appears to you, to grab an totally >meaningless capture of a, huh (?), *number* at the end of each line.

Yes I want the number at the end of the two lines in a flat text file,
the output of a simple cronjob.

The numbers might be meaningless to you, but the numbers are, in order,
the total number of emails in the mail queue and the total number of
unprocessed mails in the queue.
As totally meaningless the thought of grabbing a single number from lines >from a data file, and making an array out of it, you will extract the last item >produced by the split "my $num = (split)[-1];" (is that what its doing?).

I had my terms mixed up a bit so sue me. But the my $num = (split) [-1]
works like I wanted.

Hey Kwoody (kwooody ??), I just saw that ur on googlygooo.
I won't hold it against u this time but my reply was a follow-up to Gunnar's
and addressed him directly and referenced the OP -- you!

Since I see that u have revised your original post, I've run your follow-up
through my "Newbie-Perl-Question-Anal-Izer" perl program

\>perl Newbie-Perl-Question-Anal-Izer.pl original_post.txt thought_reorganized_post.txt

with this output:

- Poster makes a relationship based to hard coded idea that exists in his brain
- Poster is certain cron outputs exact data
- Poster does not know regular expressions

Kwoody, if its that stupidly simple, just open the fukin file and look at it,
don't start a pandemic here!!!
 
M

Matt Garrish

I put ur post in the "English Checker" program and this is what it
churrned out:

Change profession to "English teacher", is a distinct possibility!

You wrote something that works? Seems impossible. My undergrad English
degree says there is a possessive apostrophe missing from my last sentence,
however. It also says you'll never find it...

Matt
 
R

robic0

uoY etorw gnihtemos taht skrow? smeeS elbissopmi. yM dargrednu hsilgnE
eerged syas ereht si a evissessop ehportsopa gnissim morf ym tsal ecnetnes,
revewoh. tI osla syas uoy'll reven dnif ti...

Matt
huh?
 
J

Joe Smith

messages in queue: 268
messages in queue but not yet preprocessed: 0

($a,$b,$c,$num) = split(/\s+/);

Why are you splitting on whitespace?

($text_before_the_colon,$number) = split /:/;

-Joe
 
B

Brian McCauley

Jürgen Exner said:
So you want to _last_ word of each line?
my $num = (split) [-1];

I suspect it's been mentioned elsewhere in this troll-infested thread
but why not just get the last word directly.

my ($num) = /(\S+)$/;

BTW: Please do not feed the trolls. Never directly follow-up a post by
a troll. Never address any comment directly to a troll. Avoid if
possible ever mentioning a troll by name.
 
M

Mark Clements

Gunnar said:
Fast svenska skulle å andra sidan vara helt okej, tycker jag. ;-)
Gah - let down by the education system and by Babelfish in the same
sentence. Another joke I don't get :)

Mark
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Why would split() be wrong?


I agree that's a good rule of thumb, but...


And what the OP wants to throw away is everything but those digits.

"What to throw away" in Randal's rule really means "what separates items,
as opposed to items proper", not just anything you want to get rid of.
In explicit, less snappy form, it says: "If it's easier to describe what
separates the items, use split, if it is easier to describe the items
themselves, use capture".
You can get that by throwing away blanks and then accessing the n-th word
(4-th in the first case, 8-th in the second), but that's really roundabout.
Assuming a line in $_,

my ( $num) = /(\d+)$/;

will extract the number in both cases (untested).

my $num = (split)[-1];

Roundabout? To me it appears to be just as simple to figure out and
type.

It is a two-step process. First the line is broken up into words, most
of which won't be used, then the last word is extracted. A regex extracts
the desired portion from the string directly.
And isn't it actually faster to use the optimized split() rather
than invoking the regex engine in the conventional way?

That's a different aspect entirely. A normal split() involves multiple
applications of a regex. Whether split on white space is optimized I
don't know.

However, I wasn't after efficiency with my remark. It is a good thing
when the program can be made to reflect the specification closely. The
"specification" was (from the OP)

I'd like to get the number at the end of each line and put it into a
scalar so I can do something with it.

Translating that directly leads to a regex capture and not to split().

Anno
 
J

John Bokma

(e-mail address removed)-berlin.de (Anno Siegel) wrote:

[..]
Translating that directly leads to a regex capture and not to split().

Well written. I do agree with it, although it was nice to see the split
solution.
 
G

Gunnar Hjalmarsson

Anno said:
That's a different aspect entirely. A normal split() involves multiple
applications of a regex. Whether split on white space is optimized I
don't know.

Yes, it's a different aspect. Furthermore, I made a benchmark which
showed that I was wrong:

use Benchmark 'cmpthese';
our $s = "messages in queue: 268\n";
cmpthese -1, {
Capt => sub { my ($n) = $s =~ /(\d+)$/ },
Split => sub { my $n = ( split ' ', $s )[-1] },
};

Rate Split Capt
Split 297227/s -- -41%
Capt 499947/s 68% --
It is a good thing
when the program can be made to reflect the specification closely. The
"specification" was (from the OP)

I'd like to get the number at the end of each line and put it into a
scalar so I can do something with it.

Translating that directly leads to a regex capture and not to split().

While I agree that a capturing regex is the most natural solution,
claiming that split() is the _wrong_ tool is too strong to my taste.
 

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

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top