FAQ
Hello,

I have a text file in a columnar format. There are seven entries in a row,
separated by tabs.

eg.

1 23 555 34 Corporation Index Sediment

This has to be tagged as:
<no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</statu
s><value>Index</value><type>Sediment</type>

I have stored the file contents to $_ and I am using the regex

$_=~s/([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t
/<no>$1<\/no><code>$2<\/code><set>$3<\/set><id>$4<\/id><status>$5<\/status><
value>$6<\/value><type>$7<\/type>/g;

I am not able to get the text tagged. Can anyone help me out to get my
desired codes?

Thanks,
Sriram

Search Discussions

  • Brad Baxter at Jun 20, 2006 at 2:47 pm

    On 6/20/06, Sriram Rajagopalan wrote:
    Hello,

    I have a text file in a columnar format. There are seven entries in a row,
    separated by tabs.

    eg.

    1 23 555 34 Corporation Index Sediment

    This has to be tagged as:


    <no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</status><value>Index</value><type>Sediment</type>

    I have stored the file contents to $_ and I am using the regex


    $_=~s/([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t/<no>$1<\/no><code>$2<\/code><set>$3<\/set><id>$4<\/id><status>$5<\/status><value>$6<\/value><type>$7<\/type>/g;

    I am not able to get the text tagged. Can anyone help me out to get my
    desired codes?
    Your regex is expecting a tab after Sediment. Is there one?

    --
    Brad
  • Johan Vromans at Jun 20, 2006 at 3:55 pm

    Sriram Rajagopalan writes:

    1 23 555 34 Corporation Index Sediment

    This has to be tagged as:
    <no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</statu
    s><value>Index</value><type>Sediment</type>
    #!/usr/bin/perl

    use strict;
    use warnings;

    # These are the desired tags.
    my @tags = qw(no code set id status value type);

    # The input.
    my $line = "1 23 555 34 Corporation Index Sediment";

    # Split into fields.
    my @flds = split(/\t/, $line);

    # Process the tags, mapping each with an actual value into a result array, and join.
    my $result = join("",
    map { "<$_>".shift(@flds)."</$_>"} @tags);

    # Show it.
    print $result, "\n";

    Hapy hacking,

    -- Johan
  • Terrence Brannon at Jun 21, 2006 at 1:16 pm
    Why not stick it in a hash and then use XML::Generator?
    On 6/20/06, Sriram Rajagopalan wrote:

    Hello,

    I have a text file in a columnar format. There are seven entries in a row,
    separated by tabs.

    eg.

    1 23 555 34 Corporation Index Sediment

    This has to be tagged as:


    <no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</status><value>Index</value><type>Sediment</type>

    I have stored the file contents to $_ and I am using the regex


    $_=~s/([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t/<no>$1<\/no><code>$2<\/code><set>$3<\/set><id>$4<\/id><status>$5<\/status><value>$6<\/value><type>$7<\/type>/g;

    I am not able to get the text tagged. Can anyone help me out to get my
    desired codes?

    Thanks,

    Sriram
  • Geoffrey Leach at Jun 21, 2006 at 3:57 pm
    To assist you in finguring this out, you might try recoding using
    s { }{ }/x - and lay out your regex over several lines so as to better
    see what's going on. Also, if your data is consistent, (\d+)\s+ would
    be lots faster.
    On 06.20 07:19, Sriram Rajagopalan wrote:
    Hello,

    I have a text file in a columnar format. There are seven entries in a
    row,
    separated by tabs.

    eg.

    1 23 555 34 Corporation Index Sediment

    This has to be tagged as:
    <no>1</no><code>23</code><set>555</set><id>34</id><status>Corporation</statu
    s><value>Index</value><type>Sediment</type>

    I have stored the file contents to $_ and I am using the regex

    $_=~s/([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t
    /<no>$1<\/no><code>$2<\/code><set>$3<\/set><id>$4<\/id><status>$5<\/status><
    value>$6<\/value><type>$7<\/type>/g;

    I am not able to get the text tagged. Can anyone help me out to get my
    desired codes?

    Thanks,
    Sriram

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupscripts @
categoriesperl
postedJun 20, '06 at 2:20p
activeJun 21, '06 at 3:57p
posts5
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase