Modifying elements of an array

F

fenisol3

I assume the last element of my array has some junk characters attached to
it. So, how do I zap away all the junk characters after the very last
alphabet in my array? Thanks for helping out. Lets assume $headings[5] is
the last element of my array, I was thinking something like this:
"$headings1[5] =~ s/[a-zA-Z].*//;" but it doesn't work.
 
G

Gunnar Hjalmarsson

fenisol3 said:
I assume the last element of my array has some junk characters
attached to it. So, how do I zap away all the junk characters after
the very last alphabet in my array? Thanks for helping out. Lets
assume $headings[5] is the last element of my array, I was thinking
something like this: "$headings1[5] =~ s/[a-zA-Z].*//;" but it
doesn't work.

This is yet another ambigous post from you.

- What do you mean by "junk characters"?
- What might the element contain before the change?

Please study the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Also please understand that it's good manners to follow-up your posts
when people are trying to help, or else they won't be inclined to do
so again.

*Guessing* what it is you want, this may be one way:

$array[-1] =~ s/[^A-Za-z]*$//;
 
P

Paul Lalli

I assume the last element of my array has some junk characters attached to
it.

What, exactly, makes you assume this? In Perl, an array element has only
what you put into it. There are no random characters attached, like you
might expect from dealing with character arrays in C/C++.
So, how do I zap away all the junk characters after the very last
alphabet in my array?
Huh?

Thanks for helping out. Lets assume $headings[5] is
the last element of my array, I was thinking something like this:
"$headings1[5] =~ s/[a-zA-Z].*//;" but it doesn't work.


What exactly are you trying to do? If you're trying to remove all
elements after the 6th from an array, do this:

@array = @array[0..5];
or
splice (@array, 6);


If on the other hand, your sixth array element contains letters followed
by 'junk' you want to remove, you want to do this:

$array[5] =~ s/^([a-zA-Z]*).*/$1/;


Perhaps you should try to tell us again what you're trying to do, if I
didn't understand correctly...

Paul Lalli
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top