Split variable into fields ?

M

Michael Benton

I am new to perl so appoligies in advance.

I have a variable which is set to "A..A_B..B_C..C_D..D_123456"
I would like to split this into fields using the "_" as a field delimeter
(easy in awk and ksh)

Is there a way to do this in perl ? is there an equiv to teh awk "-F_" ?

Thanks

Mike
 
A

Andreas Kahari

I am new to perl so appoligies in advance.

I have a variable which is set to "A..A_B..B_C..C_D..D_123456"
I would like to split this into fields using the "_" as a field delimeter
(easy in awk and ksh)

Is there a way to do this in perl ? is there an equiv to teh awk "-F_" ?

my @fields = split(/_/, $variable);
 
J

Jürgen Exner

Michael said:
I have a variable which is set to "A..A_B..B_C..C_D..D_123456"
I would like to split this into fields using the "_" as a field
delimeter (easy in awk and ksh)

Is there a way to do this in perl ? is there an equiv to teh awk
"-F_" ?

We must be back to this time of the year where people are asking
self-anwering questions.
Please see "perldoc -f split" and/or http://www.ginini.com/perlsaq.html

jue
 
D

D Borland

Man, i've never seen such a such bunch of moaners in one place... everyone
has to throw a dig in whenever they can ... "it must be that time of year"
.... yeah, like your the all seeing all hearing to i suppose and anyone who
hasn't an intelect superiour to yours, must be picked on. What was it then
"bullied at school", "can't get it up" or just plain nastiness?

So what if people ask a question you feel is silly, doesn't give you the
right to riddicule them. Example - Say your Mum's walking down the street
in an outfit i think is stupid - Do i run over to her start laughing in her
face and publicly embarress her, start making fun of her fashion sense so me
and everyone in the street watching, can have a laugh at her expense? Well ?

I don't think you'd agree with that - (no matter what you say). If you
think someones asked a stupid question, just don't answer it. Simple
really. But no-not some of the people in here, they just can help trying to
make someone else the butt of the joke rather than themselves for once.

It's the same thing with this "top posting" crap. OK i agree that things
need to have an order, but some people put posters in the newsgroup straight
in their killfile if they do this. What happens then, if it was an accident
that they replied to the wrong thread in the post. Does that mean that
because someone might make a simple mistake then there not worthy of any
knowledge you may have? Anyhow if yous are going to act like children when
this "top posting" happens, then you should really make it clearer to first
time that your gonna take a fit and a huffy then cry and put them in your
killfile and stomp your feet for an hour or so, until someone backs you up
and says there there it's ok.

Alot of yous in here, seem to think that because you know alot about perl
that you are somehow better than people who dont so there not worthy of
anything you can help them with. So you sit and dig at them instead and put
them in your killfile. All i can say is "WHAT GOES AROUND COMES AROUND KID"
i can assure you of that.

Thank mother nature for "Kama".

Dagmar
 
M

Michael Benton

Andreas Kahari said:
my @fields = split(/_/, $variable);

Thanks. I thought I might have to use an array.

but, in ksh i can say

a=`echo $variable | awk -F_ '{print $2}'`

so could i say (on one line hopefully)

$a = $(@fields = split(/_/, $variable);)[1];

variable = field 1 of array, whose values are output of split ?

:)

Mike
 
G

Glenn Jackman

Michael Benton said:
Andreas Kahari said:
my @fields = split(/_/, $variable);

but, in ksh i can say
a=`echo $variable | awk -F_ '{print $2}'`

so could i say (on one line hopefully)
$a = $(@fields = split(/_/, $variable);)[1];

Even simpler: $a = (split /_/, $variable)[1];

$a will be undefined if the _ character does not appear in $variable, so
you should test for that before you use $a.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Man, i've never seen such a such bunch of moaners in one place...
everyone has to throw a dig in whenever they can ... "it must be that
time of year" ... yeah, like your the all seeing all hearing to i
suppose and anyone who hasn't an intelect superiour to yours, must be
picked on. What was it then "bullied at school", "can't get it up" or
just plain nastiness?

Surely you must admit that asking what is the perl operator to *split* data
up into fields is a stupid question? Or asking what the Perl equivalent to
awk's -F is?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP3HOG2PeouIeTNHoEQKhpgCgm50R1OYkB9mM1Fet5cT+dPgnzrYAoJml
D9Tp31Dh6AqgDphMY2T+9L2p
=wDJO
-----END PGP SIGNATURE-----
 
D

D Borland

Ok, i do, but there's no need to take the piss, for all we know the guy
might be mentally disabled and someone here sits and rips him, mm that's
gotta make his day seem that much brighter.

You are right though, i agree. But like i tried to say - Just don't answer
it if you think it's stupid. But hey then again who am i to tell people
right from wrong. You do whatever you want to do dude :)

Dagmar
 
U

Uri Guttman

DB> You are right though, i agree. But like i tried to say - Just
DB> don't answer it if you think it's stupid. But hey then again who
DB> am i to tell people right from wrong. You do whatever you want to
DB> do dude :)

and how does your post contributes to this thread? well, you obviously
want to volunteer to answer all newbie questions here in the most kind
and accurate way you can. we give this job to all the guys who flame
about the style of answers. you know what? they never do the job. all
they do is flame some more and then leave (us in peace). you have been
plonked by many already and haven't helped anyone at all. yet you claim
the right to flame. hahahahaha

go join moronzilla over there in the lamer line. perl and intelligent
discussion of it are beyond your skills.

uri
 
D

Dave Saville

On Wed, 24 Sep 2003 12:02:32 -0500, Eric J. Roode wrote:

Surely you must admit that asking what is the perl operator to *split* data
up into fields is a stupid question? Or asking what the Perl equivalent to
awk's -F is?

There are *no* stupid questions, only stupid answers :)

Just because the operation he wants to do and the perl operator to do
it happen, in this instance, to be the same word, does not make it a
stupid question. What if he had said "chop" the data up? It is only
"stupid" in that *the perl experts* know there is a function called
split and *the newbie* does not. So to the experts it is a funny in
joke. If he knew he would not have asked. I hope :)

And before anyone starts jumping up and down and shouting "perldoc -q",
sometimes it is not that easy to find something, especially if you have
no idea what a particular process is called in a given language - it is
the same problem with indexes in a book - if you are looking for "foo"
and the author decided it belongs under "bar" you don't stand much
chance of finding it.

Now I agree that a "perldoc -q split" was not too great a leap of
imagination in this case, but sometimes I have been unable to find the
docs for something that I *knew* was there - but how it was keyed, or
under which part of the documentation system it was hidden eluded me.
So looking for something that *might* be there can be a bit daunting.

Cut the guy a little slack - we all were beginners once and hopefully
we never stop learning. Don't forget that to the newbie a FAQ isn't -
it is something he does not understand or know how to do. It is only a
FAQ to the rest of us. Be gentle and they will most likely find it
themselves next time. Now if they *keep* coming back with essentially
the same type of question - that is an entirely different matter and
they need jumping on.

Just my 2cents.

Regards

Dave Saville

NB switch saville for nospam in address
 
S

Sam Holden

On Wed, 24 Sep 2003 12:02:32 -0500, Eric J. Roode wrote:



There are *no* stupid questions, only stupid answers :)

Just because the operation he wants to do and the perl operator to do
it happen, in this instance, to be the same word, does not make it a
stupid question. What if he had said "chop" the data up? It is only
"stupid" in that *the perl experts* know there is a function called
split and *the newbie* does not. So to the experts it is a funny in
joke. If he knew he would not have asked. I hope :)

If he said chop it wouldn't be self answering, since when he looked up
"chop" he wouldn't have found the answer, and hence asking her would be
appropriate.

Doing a search for the terms you use in the question is expected,
otherwise you come across as a selfish prat who believes their time is
*so* valuable, that spending 30 seconds of their time searching isn't
worth saving hundreds or thousands of people all over the world the
time it takes them to read the post.
And before anyone starts jumping up and down and shouting "perldoc -q",
sometimes it is not that easy to find something, especially if you have
no idea what a particular process is called in a given language - it is
the same problem with indexes in a book - if you are looking for "foo"
and the author decided it belongs under "bar" you don't stand much
chance of finding it.

Now I agree that a "perldoc -q split" was not too great a leap of
imagination in this case, but sometimes I have been unable to find the
docs for something that I *knew* was there - but how it was keyed, or
under which part of the documentation system it was hidden eluded me.
So looking for something that *might* be there can be a bit daunting.

The entire point is that the *exact* terms to use are in the
question. That is what makes a "self answering question".

If the question was "how do I seperate out the components of a line
of data, such as '...'" it wouldn't be self answering, and hence wouldn't
generate that style of reply. Instead the answers would range from
detailed explanations of how to do it with index by those afraid of
anything "above" that, to sample code using split, to "perldoc -f split".

With the last type being the most useful, in my opinion anyway.
Cut the guy a little slack - we all were beginners once and hopefully
we never stop learning. Don't forget that to the newbie a FAQ isn't -
it is something he does not understand or know how to do.

Except that reading the FAQ *before* posting is part of usenet culture.
Nothing specific to comp.lang.perl.misc there. Not following the
practices of a community when in that community is going to cause social
problems for you.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 24 Sep 2003 12:02:32 -0500, Eric J. Roode wrote:



There are *no* stupid questions, only stupid answers :)

Just because the operation he wants to do and the perl operator to do
it happen, in this instance, to be the same word, does not make it a
stupid question. What if he had said "chop" the data up? It is only
"stupid" in that *the perl experts* know there is a function called
split and *the newbie* does not. So to the experts it is a funny in
joke. If he knew he would not have asked. I hope :) ....

Cut the guy a little slack - we all were beginners once and hopefully
we never stop learning. Don't forget that to the newbie a FAQ isn't -
it is something he does not understand or know how to do. It is only a
FAQ to the rest of us. Be gentle and they will most likely find it
themselves next time. Now if they *keep* coming back with essentially
the same type of question - that is an entirely different matter and
they need jumping on.

Let me make one thing clear: When I opined that it was a stupid
question, that was not the voice of an elitist Perl snob degrading a
newbie. Yeah, I'm a freakin' expert in Perl (and yes, with elitist snob
leanings! ;-) ), but I am a newbie in other areas, and I participate in
newsgroups in which I am a newbie as well as newsgroups in which I am an
expert, and have for many years.

If you search Google for posts I've made in c.l.p.m., you will find that
I do quite frequently answer questions that are clearly posted by
novices, and I do so in a polite, non-condescending manner.

The OP's question:

# I have a variable which is set to "A..A_B..B_C..C_D..D_123456"
# I would like to split this into fields using the "_" as a field
# delimeter (easy in awk and ksh)
#
# Is there a way to do this in perl ? is there an equiv to the
# awk "-F_" ?

is not merely a pair of questions from a novice. It is a pair of
questions from a novice who clearly has not bothered to do the slightest
bit of research -- either on the web, newsgroup archives, any Perl book
at any bookstore or library, NOTHING.

It is reasonable to suppose that this is a person who has little interest
in self-improvement, just easy answers. I see nothing morally wrong with
flaming such a person -- discouraging such people from posting does
marginally improve the average article quality on c.l.p.m. :)

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP3JDKmPeouIeTNHoEQIjIQCgyKF4uLr6Vb0TPvthr8isHOqYhZYAn1+k
WegEnu4nrbJi/AO0cFyTDSiy
=mv4M
-----END PGP SIGNATURE-----
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc]

I am new to perl so appoligies in advance.

I have a variable which is set to "A..A_B..B_C..C_D..D_123456"
I would like to split this into fields using the "_" as a field delimeter
(easy in awk and ksh)

Is there a way to do this in perl ? is there an equiv to teh awk "-F_" ?

Thanks

Mike

$string = "A..A_B..B_C..C_D..D_123456";
@fields = split(/_/,$string);
 

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,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top