?
Solved

Split

Posted on 2000-04-23
14
Medium Priority
?
181 Views
Last Modified: 2010-03-05
Is there anyway to make 'split' work just like 'cut' of shell script?

I would like get data from column 2 and 4.

col1   col2     col3     col4  
xxx    xxx      xxx      xxxx
ddd    ddd      ddd      dddd
fff    ddd      sss      rrrr

Thanks.
0
Comment
Question by:sata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 4

Expert Comment

by:adam923
ID: 2742157
open(INFILE, "<textfile");
while(<INFILE>){
  @cols = split;
   # do stuff with $cols[1] and $cols[3] (columns 2 and 4)
}
close INFILE;
0
 
LVL 84

Expert Comment

by:ozo
ID: 2742269
$cut = join'\t',(split)[1,3]; # cut -f2,4
0
 
LVL 1

Author Comment

by:sata
ID: 2742514
Hi,

sorry that I'm not very good with Perl,
I would like to ask, isn't split's syntax something like 'split(@array)? What does "join'\t',(split)[1,3]" means?

Thank you very much for the comments...

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 2742530
perldoc -f split
=item split /PATTERN/,EXPR,LIMIT

=item split /PATTERN/,EXPR

=item split /PATTERN/

=item split

Splits a string into an array of strings, and returns it.  By default,
empty leading fields are preserved, and empty trailing ones are deleted.
0
 
LVL 4

Expert Comment

by:adam923
ID: 2742654
To explain my example, split in array context with no arguments splits the default variable ($_ which now refers to the current line of text) into fields separated by whitespace.
0
 
LVL 1

Author Comment

by:sata
ID: 2742897
Am I right to say that the data
col1   col2     col3     col4    
xxx    xxx      xxx      xxxx
ddd    ddd      ddd      dddd
fff    ddd      sss      rrrr

will become a long string, something like:

col1 col2 col3 col4 xxx xxx xxx xxxx ddd ddd ddd dddd

if I need to access the 'xxx' data of column1, I need to (after split) specify "array[4]" to retrive the data?

So it doesn't really work like 'cut', isn't it?
0
 
LVL 4

Expert Comment

by:adam923
ID: 2742907
in my example the data is delt with one line at a time
one could expand this to keep track of each column separately

open(INFILE, "<textfile");
while(<INFILE>){
     @cols = split;
     push @col2, $cols[1];
     push @col4, $cols[3];
}
close INFILE;

Now the array @col2 = (col2, xxx, ddd, ddd) and the array @col4 = (col4, xxxx, dddd, rrrr);
0
 
LVL 1

Author Comment

by:sata
ID: 2742997
Adjusted points from 50 to 100
0
 
LVL 1

Author Comment

by:sata
ID: 2742998
you are using a file as your input, so if I used a variable:

e.g.

$list1 = `ls -l`;

while($list1) {
     @cols = split;
     push @col2, $cols[1];
     push @col4, $cols[3];
}

will the system still take in the data line by line?
Sorry for all the trouble caused.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2743006
#if you used an array:
@list1 = `ls -l`;
foreach( @list1 ){
#it would take the data line by line.
}
#but if you want to take columns from `ls -l`, you might consider using the built in stat function.
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 2743014
#or
foreach( split/\n",$list1 ){
    @cols = split;
    push @col2, $cols[1];
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 2743015
foreach( split/\n/,$list1 ){
0
 
LVL 1

Author Comment

by:sata
ID: 2743755
Adam has been a great help too, is there anyway I can give points to him too?
0
 
LVL 4

Expert Comment

by:adam923
ID: 2743990
you could post another ? but don't bother, thanks for the offer though
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question