Easy Field Grabbing Question

  • Thread starter Geezer From Freezer
  • Start date
G

Geezer From Freezer

I've had a look in some books but can't find what I'm looking for.

I have a line of text in a variable or several in an array
and I want to extract the 5th field of each line - how?
 
A

Anno Siegel

You advertise your question as an "easy" one. How do you know it is
easy when you don't know the solution?
I've had a look in some books but can't find what I'm looking for.

I have a line of text in a variable or several in an array
and I want to extract the 5th field of each line - how?

That can't be answered before you specify how fields are separated
in the text.

Anno
 
G

Geezer From Freezer

Anno said:
You advertise your question as an "easy" one. How do you know it is
easy when you don't know the solution?

I don't - but it's easy in awk, which I don't want to use for this
situation

That can't be answered before you specify how fields are separated
in the text.

fields are space seperated. Not necessarily 1 space, but could be 1 or more
spaces.
 
A

Anno Siegel

Geezer From Freezer said:
I don't - but it's easy in awk, which I don't want to use for this
situation



fields are space seperated. Not necessarily 1 space, but could be 1 or more
spaces.

my $fifth = (split)[4];

Anno
 
G

Geezer From Freezer

Anno said:
my $fifth = (split)[4];

Anno

ok another stupid question time!

how does the above work for an array?? $fifth is being created but
does not reference the array?

I've tried the following:

foreach $user (@list){

print "$user"; #output prints fine!
my $fifth = (split $user[4]);
print "$fifth"; # gets 0 every time!
}
 
A

Anno Siegel

Geezer From Freezer said:
Anno said:
my $fifth = (split)[4];

Anno

ok another stupid question time!

how does the above work for an array?? $fifth is being created but
does not reference the array?

You really need to make your questions clearer.

The plain answer is, the above doesn't work for an array, and isn't meant
to.

Explain *how* an array comes into play.
I've tried the following:

foreach $user (@list){

print "$user"; #output prints fine!
my $fifth = (split $user[4]);
^^^^^^^^
This presupposes an array @user, which is nowhere defined. You should
run all your code under "use strict", it would have picked up your error.
Also, it assigns the result of split to a scalar, which is not what you
want. Have you read "perldoc -f split" at all? You should do that
*before* presenting questions to the group.
print "$fifth"; # gets 0 every time!
}

Apparently you expect an array of strings that all must be subjected
to the split/extract-fifth-element procedure. For that, you don't
need to change the code at all, just wrap a loop around it. Untested:

for ( @list ) {
my $fifth = (split)[4];
# do something with $fifth
}

Anno
 
A

Andras Malatinszky

Geezer said:
Anno said:
my $fifth = (split)[4];

Anno


ok another stupid question time!

how does the above work for an array?? $fifth is being created but
does not reference the array?

I've tried the following:

foreach $user (@list){

print "$user"; #output prints fine!
my $fifth = (split $user[4]);
print "$fifth"; # gets 0 every time!
}

Well what did you expect? When you say $user[4] you are creating the
@user array from out of thin air (using strict would have warned you
about that), so $user[4] will be 0 (along with every other element of
@user) and if you split it, you still get 0.

Instead of randomly combining functions in the hope that you will get
your desired result by chance, take Gunnar Hjalmarsson's advice and read
up on the split function.
 
A

A. Sinan Unur

Anno said:
my $fifth = (split)[4];

Anno

ok another stupid question time!

how does the above work for an array?? $fifth is being created but
does not reference the array?

I've tried the following:

foreach $user (@list){

print "$user"; #output prints fine!
my $fifth = (split $user[4]);
print "$fifth"; # gets 0 every time!
}

Joining this thread a little late, and at the risk of being
misunderstood, I am going to suggest that you read the posting guidelines
for this group. The document is available on the WWW at
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html, and is
posted here regularly (thank you Tad).

Reading that document will alert you to the fact that Perl comes with
searchable documentation accessible via the perldoc command (on Windows,
you also get the docs in HTML format). It will also point out that you
should at least have:

use strict;
use warnings;

in your programs.

While, with perldoc, it can sometimes be a little hard to figure out how
to reach the specific bit of information you are seeking, in your case,
it would have been easy to figure out since you are looking for a way of
splitting text into separate fileds. Hence:

perldoc -f split

In most cases, it is far easier and quicker to figure things out using
tools that are readily available on your computer.

Now, the subtle change you made to Anno's answer indicates you are not on
solid ground with your Perl knowledge. I suggest you invest in a book
first, and learn some of the basics first. Remember, one step at a time.

Sinan.
 

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,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top