I have a file which contains information something like this:
TAG1 "file1.txt"
some additional lines
TAG2 "file2.txt"
some more lines
TAG3 "file3.txt".
Now, I want to read what is inside the double quotes and assign it to variable ( something like $var1 = file1.txt
$var2 = file2.txt
$var3 = fil3.txt)
. Can anyone guild me how to do this.?
You could achieve your goal by
using regular expression
my @files; while (my $line = <>) { if (m/"([^"]+)"/) { push @files, $1; } }
using split()
my @files; while (my $line = <>) { my (undef, $file, undef) = split /"/, $line, 3; push @files, $file; }
Except for the period after "file3.txt".
(which I suspect is a artifact from posting the question), your data appears to be a CSV file with tabs.
If that's the case, I advise you to parse the file with Text::CSV
use strict;
use warnings;
use autodie;
use Text::CSV;
my $csv = Text::CSV->new ( { sep_char => "\t" } )
or die "Cannot use CSV: ".Text::CSV->error_diag ();
my @files;
open my $fh, '<', 'file.csv';
while ( my $row = $csv->getline( $fh ) ) {
push @files; $row->[1];
}
$csv->eof or $csv->error_diag();
close $fh;
print "@files";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.