Re: Using regular expressions to insert tags

[email protected] (Johan Vromans) Tue, 20 Jun 2006 17:54:58 +0200
Newsgroups perl.scripts
Message-ID <[email protected]>
Sriram Rajagopalan <[email protected]> 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