newtoperl1 has asked for the wisdom of the Perl Monks concerning the following question:

Team,

I have a file which has lines like below.lets just call it result.txt

a.b.c.d=10 a.b.e.f=9999 a.f.g.h=456

I am not worried about anything thats before the "=" symbol. I only need values after "=" in my final file.like final.txt

any suggestions please? Appreciate your help.

Replies are listed 'Best First'.
Re: I need just the value
by kennethk (Abbot) on Jun 01, 2009 at 20:17 UTC
    In Perl there are a number of ways you could accomplish this task.
    1. You could use substr, since every element you have listed has the same number of characters before the '=' sign.
    2. You could use split, splitting on the '=' and keeping only the second half.

      my $value = (split /\=/, $string)[1];

    3. You could use a capturing regular expression to store all characters after the equals sign.

      my ($value) = $string =~/\=(.*)$/;

    4. You could use a regular expression to substitute the leading characters away.

      $string =~ s/^[^=]*\=//;

    I'm sure people can come up with a host of other approaches, but this should give you some ideas.

    Update: Fixed typos in #4. Thanks ikegami.

Re: I need just the value
by ikegami (Patriarch) on Jun 01, 2009 at 20:16 UTC
Re: I need just the value
by Marshall (Canon) on Jun 01, 2009 at 20:19 UTC
    Try this:
    #!/usr/bin/perl -w use strict; while (<DATA>) { my $number = (/(\d+)$/)[0]; #previous was \w+ oooops! print "$number\n" if defined $number; # "if defined" allows for blank lines, etc. } __DATA__ a.b.c.d=10 a.b.e.f=9999 a.f.g.h=456 #prints: #10 #9999 #456
    Update: some explanation:
    my $number = (/(\d+)$/)[0];
    the (/(\d+)$/) part is a "regular expression" and says to look for a sequence of digits (0-9) right before the end of the string. This is captured into an "automagic" variable called $1, but $1 doesn't have much meaning, so this regex is put into a list context and a "list slice" is used so that I can assign $1 to the variable $number which presumably will have meaning if you give it a better name in your application. The "if defined" part skips the print if for some reason this number is not found (blank line, malformed line), etc.
Re: I need just the value
by bichonfrise74 (Vicar) on Jun 01, 2009 at 20:51 UTC
    Try this...
    #!/usr/bin/perl use strict; while (<DATA>) { my $val = (split /=/)[1]; print $val; } __DATA__ a.b.c.d=10 a.b.e.f=9999 a.f.g.h=456
Re: I need just the value
by CountZero (Bishop) on Jun 02, 2009 at 09:05 UTC
    Or everything together in (basically) one line:
    use strict; use warnings; print join '/', map {chomp;(split /=/)[-1]} <DATA>; __DATA__ a.b.c.d=10 a.b.e.f=9999 a.f.g.h=456

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: I need just the value
by newtoperl1 (Initiate) on Jun 01, 2009 at 23:37 UTC
    Thank you!it worked.

    I have another question.

    now my final.txt looks like below
    10 9999 456
    how can I saperate these values by a delimiter, so I get my ouputfile,say output.txt, like below?
    10/9999/456
      Another possibility would be using join, a la:

      print join '/', @array;

      #!/usr/bin/perl use strict; use warnings; # 767362 my $out_line; my @line = <DATA>; for my $line (@line) { chomp $line; $out_line .= ( $line . "/" ); } if ( $out_line =~ /(.*)\/$/ ) { # capture everything before a slash i +mmediately preceding EOL $out_line = $1; # and overwrite $out_line with what you c +aptured } print $out_line . "\n"; __DATA__ 10 9999 456 ...

      Output:   10/9999/456/...

      See perlretut and Tutorials. (And, as may be obvious, it would probably be smarter to incorporate this into the previous solution, rather than create a separate script)

        That looks very complicated. Simpler:
        while (<>) { print("/") if $. != 1; print(/=(.*)/); } print("\n");

        (Works from your original input)


        Update: By the way, the following is wasteful:

        my @line = <DATA>; for my $line (@line) {

        It loads the entire file into memory for nothing. Use the following instead:

        while (my $line = <DATA>) {