LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2010, 04:17 PM   #1
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Rep: Reputation: 16
Perl Split


Hello -

I am trying to split a pattern in perl and so far I cannot get it to work. I have a pattern that looks like this:

76|2455311|2455312|00:00:00|00:00:00|Once|0|Donkey | |

I always need to print out the 8th field. So in this case donkey. Here is what I have so far:

Code:
#!/usr/bin/perl

  use strict;
  use warnings;
open (F, "data.pl") || die "Could not open data.pl: $!\n";
my @test = <F>;
close F;

  my @values = split('|', @test);

  foreach my $val (@values) {
    print "$val\n";
  }

  exit 0;
Any help would be greatly appreciated!

Tarken
 
Old 05-17-2010, 04:52 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tarken View Post
...
I cannot get it to work.
...
And what exactly doesn't work ?

...

By the way, did you read

perldoc -f split

?

Last edited by Sergei Steshenko; 05-17-2010 at 04:54 PM.
 
Old 05-17-2010, 04:59 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Even more specifically, did you carefully line #4 of 'perldoc -f split' ?

In my case the line is:


Code:
      4        split   Splits the string EXPR into a list of strings and returns that list.  By default, empty leading fields are preserved, and empty trailing ones are deleted.
      5                (If all fields are empty, they are considered to be trailing.)

Last edited by Sergei Steshenko; 05-17-2010 at 05:09 PM.
 
Old 05-17-2010, 06:30 PM   #4
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
And what exactly doesn't work ?

...
When I run that script on the data that I provided I only get the number 4. I want it to display donkey, because it is the 8th spot. I need this to run on a file full of patterns like the one I provided. I did read through the perldoc, just not understanding it.


Tarken
 
Old 05-17-2010, 06:44 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tarken View Post
When I run that script on the data that I provided I only get the number 4. I want it to display donkey, because it is the 8th spot. I need this to run on a file full of patterns like the one I provided. I did read through the perldoc, just not understanding it.


Tarken
No, it's not that you do not understand 'perldoc', it is that you do not analyze the text properly.

So, here is a simpler task for you - first an English sentence to be analyzed:

Jack climbs a high mountain.

The sentence in italics contains the following items:
  1. an entity performing an action;
  2. an entity describing the action;
  3. an entity over/on which the action is performed.

So, first of all, which are the three entities ?

Last edited by Sergei Steshenko; 05-17-2010 at 08:21 PM.
 
Old 05-19-2010, 02:17 PM   #6
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
No, it's not that you do not understand 'perldoc', it is that you do not analyze the text properly.

So, here is a simpler task for you - first an English sentence to be analyzed:

Jack climbs a high mountain.

The sentence in italics contains the following items:
  1. an entity performing an action;
  2. an entity describing the action;
  3. an entity over/on which the action is performed.

So, first of all, which are the three entities ?
The man, climbing, mountain.
 
Old 05-19-2010, 06:20 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tarken View Post
The man, climbing, mountain.
Good (almost). Though, strictly saying, not "the man", but "Jack", and not "climbing", but "climbs". Now, what kind/type of mountain the man climbs ?

And after answering that question perform the same analysis of already quoted here

Code:
      4        split   Splits the string EXPR into a list of strings and returns that list.  By default, empty leading fields are preserved, and empty trailing ones are deleted.
line, including answer to the

what kind of the entity on/over which the action is performed ?

question.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl split whitespace sharky Programming 5 10-23-2008 10:47 AM
Using split in perl xjlittle Programming 9 06-21-2007 07:13 AM
Perl split ShaqDiesel Programming 5 08-17-2006 08:02 PM
Perl Help (possible split) fooforon Programming 2 02-19-2004 05:53 AM
PERL: split on html tag? ocularbob Programming 12 09-08-2003 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration