in reply to How to get a value from a file and save in another file

How about

print("LA-1-00 LA-2-01 LA-03-2\n");

Sorry, but I don't see the pattern between your input and output. Did you mean to print out all those starting with LA-? If so, you missed LA-03-02. The following prints all the tokens starting with LA-

open(FILE, "< $filename") or die("Can't open input file: $!\n"); my $text = do { local $/; <FILE> }; close(FILE); print(join(' ', grep { /^LA-/ } split(/\s+/, $text)), "\n");

Update: Maybe LA-03-2 and LA-03-02 are considered duplicates, and you're trying to remove duplicates? If so, the following code should help:

open(FILE, "< $filename") or die("Can't open input file: $!\n"); my $text = do { local $/; <FILE> }; close(FILE); my %seen; print(join(' ', grep { if (/LA-(\d+)-(\d+)/) { my $key = "LA-" . ($1+0) . "-" . ($2+0); $seen{$key}++ ? 0 : 1; } else { 0 } } split(/\s+/, $text)), "\n");

Replies are listed 'Best First'.
Re^2: How to get a value from a file and save in another file
by Anonymous Monk on Apr 27, 2005 at 17:34 UTC
    Thank you for the help. I want to print out the first value that start with "LA-" for each record. In my example file there are 3 records and each record has 1 or 2 or more values. In my real file there Hundreds records.
      use strict; use warnings; my $found_match = 0; while (<DATA>) { s/\s+$//; if (/^\d/) { $found_match = 0; } if (!$found_match && /^\d*\s+(LA.*)/) { print("$1\n"); $found_match = 1; } } __DATA__ 1 LA-1-00 2 CON-1-0 LA-2-01 3 LA-03-2 LA-03-02