Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

newbie question: extracting elements from a string

eddie_pheddie_ph Member Posts: 1
[b][red]This message was edited by the eddie_ph at 2002-7-2 16:55:21[/red][/b][hr]
Hi,

I'm new to Perl... but I need to use it to analyse some data files. I've figured out a lot of the basics and have no problems reading the lines of data from the files etc.

The problem I'm having is with interpreting the data in one of the files. I need to extract the 1st and 3rd fields of data on the line. I was expecting the data to be comma separated and was all prepared to use...

split(",", $_);

on each line of data. Unfortunately there is also additional extraneous whitespace on each line of the file. For example, given the line...

[Code]CustomerID123456 ,LicenseID654321, ProductID321,$12.00[/Code]

I need the values 'CustomerID123456' and 'ProductID321'.

Can anyone help me with this. The simplest and easiest solution to read this would be much appreciated because I'm having difficulty.

Basically, the question is how can I get the 4 tokens from the above line?

Thanks in advance for your help.

Eddie


Comments

  • bgibbybgibby Member Posts: 251
    : [b][red]This message was edited by the eddie_ph at 2002-7-2 16:55:21[/red][/b][hr]
    : Hi,
    :
    : I'm new to Perl... but I need to use it to analyse some data files. I've figured out a lot of the basics and have no problems reading the lines of data from the files etc.
    :
    : The problem I'm having is with interpreting the data in one of the files. I need to extract the 1st and 3rd fields of data on the line. I was expecting the data to be comma separated and was all prepared to use...
    :
    : split(",", $_);
    :
    : on each line of data. Unfortunately there is also additional extraneous whitespace on each line of the file. For example, given the line...
    :
    : [Code]CustomerID123456 ,LicenseID654321, ProductID321,$12.00[/Code]
    :
    : I need the values 'CustomerID123456' and 'ProductID321'.
    :
    : Can anyone help me with this. The simplest and easiest solution to read this would be much appreciated because I'm having difficulty.
    :
    : Basically, the question is how can I get the 4 tokens from the above line?
    :
    : Thanks in advance for your help.
    :
    : Eddie
    :
    :
    :

    Ok, your answer depends upon how you wish to handle the spaces!

    If you don't want them then excellent, just use a Regular Expression to remove them like this:-

    [code]

    my @values = split(/,/, $_);

    foreach my $item (@values) {
    $item =~ s/s//g;
    }

    [/code]

    This will remove EVERY white space character from the field (inc. Tabs, Spaces, etc.).

    If you have a field like "Customer ID 123456" then this will become "CustomerID123456". This is sometimes NOT what you want at all.

    If you want to preserve just single spaces between words and keep "Customer ID 123456" when you have a field like "Customer ID 123456 " then a small modification to the above code like this will do it!

    [code]

    my @values = split(/,/, $_);

    foreach my $item (@values) {
    $item =~ s/s{2,}//g;
    }

    [/code]

    This just says, substitue any two or more spaces within my string with nothing!

    Hope this helps!
    Bradley q:)
  • Shawn CarterShawn Carter Member Posts: 0

    _____ { http://forcoder.org } free ebooks and video tutorials about | C, Ruby, C++, Swift, Python, C#, Visual Basic, Perl, JavaScript, Assembly, Visual Basic .NET, Java, MATLAB, Objective-C, PHP, Go, Scratch, R, Delphi, PL/SQL Scheme, Awk, VBScript, Kotlin, Lua, Rust, Crystal, Hack, Scala, Clojure, Apex, ML, Erlang, SAS, Lisp, Alice, COBOL, Transact-SQL, D, F#, Ada, Fortran, LabVIEW, Prolog, Dart, Logo, FoxPro, Bash, Julia, ABAP | ______________

Sign In or Register to comment.