Turning a list of scalars into an array?

K

kwoody

I know what I want to accomplish here but can not figure it out. Open a
file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed: 0

Before when it only had one line I'd just use a split(). Now Ive got
two lines so a:

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

wont work with the second line, only the first.

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.

Ive tried various iterations of split/splice and a few others but just
cant figure this one.

Anyone have a suggestion?
Thanks,
Keith
 
J

Jürgen Exner

I know what I want to accomplish here but can not figure it out. Open
a file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed:
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.

So you want to _last_ word of each line?
Simple. Just split() the line into words and then grab the last element:

for (@xxx) {
my $num = (split) [-1];
print $num;
}

jue
 
R

robic0

I know what I want to accomplish here but can not figure it out. Open a
file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed: 0

Before when it only had one line I'd just use a split(). Now Ive got
two lines so a:

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

wont work with the second line, only the first.

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.

Ive tried various iterations of split/splice and a few others but just
cant figure this one.

Anyone have a suggestion?
Thanks,
Keith


I ran your post through my Perl-Newbie_Question-Analyzer perl program,
here's what I got:

- 2 lines of text.
- Its in a file.
- You want to get the number into a "scalar" (huh?).
- You can't figure this out.

Is that right?
 
A

Anno Siegel

I know what I want to accomplish here but can not figure it out. Open a
file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed: 0

Before when it only had one line I'd just use a split(). Now Ive got
two lines so a:

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

wont work with the second line, only the first.

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.

Ive tried various iterations of split/splice and a few others but just
cant figure this one.

You're using the wrong tool. 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."

In your case, what you want to keep it the digits at the end of each line.
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).

Anno
 
K

kwoody

Yes that was it. I looked at various examples of split() but that
syntax eluded me.

Thanks,
Keith
 
R

robic0

I know what I want to accomplish here but can not figure it out. Open
a file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed:
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.

So you want to _last_ word of each line?
Simple. Just split() the line into words and then grab the last element:

for (@xxx) {
my $num = (split) [-1];
print $num;
}

jue
Ha, that don't make sense. Why would, and what good would
an array of line number indexed, numbers be to a normal human?
 
J

Jürgen Exner

Yes that was it. I looked at various examples of split() but that
syntax eluded me.

What was what? Which syntax?

Please quote an appropriate amount of text -as has been customary on Usenet
for the past 2 decades- such that people have a chance to know what you are
talking about.

jue
 
R

robic0

I know what I want to accomplish here but can not figure it out. Open a
file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed: 0

Before when it only had one line I'd just use a split(). Now Ive got
two lines so a:

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

wont work with the second line, only the first.

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.

Ive tried various iterations of split/splice and a few others but just
cant figure this one.

You're using the wrong tool. 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."

In your case, what you want to keep it the digits at the end of each line.
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).

Anno

I'll say it again, it gives an array of monolithic data, not related to
anything at all:

That don't make sense. Why would, and what good would
an array of line number indexed, numbers be to a normal human?

Sometimes Anno, you have to ponder something else other than your naval!
 
J

Jürgen Exner

robic0 said:
I know what I want to accomplish here but can not figure it out.
Open a file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:
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.

So you want to _last_ word of each line?
Simple. Just split() the line into words and then grab the last
element:

for (@xxx) {
my $num = (split) [-1];
print $num;
}

Ha, that don't make sense.

Did you mean "doesn't" instead of "don't"?
Well, did you try it? Amazingly enough this piece of code prints the
trailing numbers from each line, just as the OP asked for.
If you don't understand the code then you are very welcome to ask for an
explanation.
Why would, and what good would
an array of line number indexed, numbers be to a normal human?

"Syntax error. General parser error in parsing English sentence."
Would you mind translating this sentence of yours into English?

jue
 
Y

ykram

I know what I want to accomplish here but can not figure it out. Open a
file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:

print $xxx[0] prints out: messages in queue: 268
print $xxx[1] prints out: messages in queue but not yet preprocessed: 0

Before when it only had one line I'd just use a split(). Now Ive got
two lines so a:

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

wont work with the second line, only the first.

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.

If you must use split (and not regular expressions) and you know that the
lines you want always comes in that order, you could do the following:

my $queue = (split /\s+/, $xxx[0])[-1];

Using a negative index retrieves values from the end of the array.
See your manual page on perldata for details, or visit
http://perldoc.perl.org/perldata.html
Anyone have a suggestion?

I wouldn't use split in this case. Perhaps now is a good time for you
to look at regular expressions: http://perldoc.perl.org/perlre.html

Good luck
ykram
 
R

robic0

robic0 said:
(e-mail address removed) wrote:
I know what I want to accomplish here but can not figure it out.
Open a file from disk, it has two lines:

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

Those two lines stored in @xxx, hence a:
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.

So you want to _last_ word of each line?
Simple. Just split() the line into words and then grab the last
element:

for (@xxx) {
my $num = (split) [-1];
print $num;
}

Ha, that don't make sense.

Did you mean "doesn't" instead of "don't"?
Well, did you try it? Amazingly enough this piece of code prints the
trailing numbers from each line, just as the OP asked for.
If you don't understand the code then you are very welcome to ask for an
explanation.
Why would, and what good would
an array of line number indexed, numbers be to a normal human?

"Syntax error. General parser error in parsing English sentence."
Would you mind translating this sentence of yours into English?
Web4, u be Swedish. Have to turn on my Sweede translator perl program.
 
K

kwoody

for (@xxx) {
my $num = (split) [-1];
print $num;
}

What was what? Which syntax?

====

The syntax of (split) [-1]

Please quote an appropriate amount of text -as has been customary on
Usenet for the past 2 decades- such that people have a chance to know
what you are talking about.

===

Oops, sorry. Been posting to Usenet for a long time off and on but am
using Google Groups for the first time to post and did not see that a
Reply doesnt quote text.

Thanks again, the split() you showed above worked nicely.
 
A

A. Sinan Unur

robic0 wrote:
....


"Syntax error. General parser error in parsing English sentence."
Would you mind translating this sentence of yours into English?

It has already been established that it does not speak or understand
English. ;-)

Sinan
 
G

Gunnar Hjalmarsson

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?
 
M

Mark Clements

robic0 said:
Web4, u be Swedish. Have to turn on my Sweede translator perl program.

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

Mark
 
J

Jürgen Exner

Oops, sorry. Been posting to Usenet for a long time off and on but am
using Google Groups for the first time to post and did not see that a
Reply doesnt quote text.

Shamelessly stolen from someone else:

For [...] and everybody else with "User-Agent: G2/#.#":

"How can I automatically quote the previous message
when I post a reply?"
http://groups.google.co.uk/support/bin/answer.py?answer=14213

See also:
http://www.safalra.com/special/googlegroupsreply/


What's good 'netiquette' when posting to Usenet?
http://groups.google.co.uk/support/bin/answer.py?answer=12348
http://directory.google.com/Top/Computers/Usenet/Etiquette/

But Google needs you to vote for 'Default quoting of
previous message in replies'
http://groups-beta.google.com/support/bin/request.py?contact_type=features




jue
 
R

robic0

It has already been established that it does not speak or understand
"It will put the lotion in the basket".
"It will put the lotion in the basket".
"It will put the lotion in the basket".
....
....
"Put the fuckin lotion in the basket lady!!"
 
M

Mark Clements

robic0 said:
Did you understand that or not?
--Sweedish--

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

Mark
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top