Home > Archive > PERL Beginners > July 2005 > get value (name of file)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
get value (name of file)
|
|
| Brent Clark 2005-07-24, 8:29 pm |
| Hi list
I was hoping someone would be so kind as to help me with this.
I have a CSV file and on every line, I have a line like so:
lts.dat|somedata
lts001.dat|somemoredata
I basically would like to ONLY get the name of the file before the first
pipe.
I was thinking that the regex would look something like so
/^*\|/ # Get the beginning of the CSV file, extract the filename, to
#the first pipe
but I need to extract and pass the value to a variable.
Would anyone have any tips or advise on this.
Kind Regards
Brent Clark
| |
| Rob Coops 2005-07-24, 8:29 pm |
| Try the following (this is just a short example)
#!/usr/bin/perl -w
use strict;
# Open the file
open (IN, "<your_file.csv") or die "Oops unable to read the file error: " .
$!;
# Loop thru the fill till the bitter end
while (<IN> ) {
chomp; # Make sure there is no funny stuff at the end (not really needed in
this example though)
my ($temp) = $_ =~ /^(.*)\|.*$/; ($_ holds the line so we match the stuff
we want to have and put it in $temp)
print $temp . "\n"; # because I had no other things to do with this just
print it to screen
}
close IN; # Close the file.
Note that both warnings and strict is used in this application just as a for
of good practice, at first it will make things more difficult but soon
enough you will see that any good perl programer will not even start typing
without enabeling both.
On 7/21/05, Brent Clark <bclark@eccotours.dyndns.org> wrote:
>
> Hi list
>
> I was hoping someone would be so kind as to help me with this.
>
> I have a CSV file and on every line, I have a line like so:
>
> lts.dat|somedata
> lts001.dat|somemoredata
>
> I basically would like to ONLY get the name of the file before the first
> pipe.
>
> I was thinking that the regex would look something like so
>
> /^*\|/ # Get the beginning of the CSV file, extract the filename, to
> #the first pipe
>
> but I need to extract and pass the value to a variable.
>
> Would anyone have any tips or advise on this.
>
> Kind Regards
> Brent Clark
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Thomas Bätzler 2005-07-24, 8:29 pm |
|
Brent Clark <bclark@eccotours.dyndns.org> asked:
> I have a CSV file and on every line, I have a line like so:
>
> lts.dat|somedata
> lts001.dat|somemoredata
Generally, you can capture data from a RE with a pair
of brackets around the relevant expression, and the
data is then available in the variables $1..$9 for up
to 9 pairs of brackets. You can also use the fact that
a match in list context will return the catured fields
as a list like this:
open( IN, $csvfile ) or die "Can't open '$csvfile': $!";
while( my $line = <IN> ){
if( my( $filename ) = ( $line =~ m/^(.*?)\|/ ) ){
print "file: $filename\n";
} else {
warn "line '$line' did not match\n";
}
}
HTH,
Thomas
| |
| Tom Allison 2005-07-24, 8:29 pm |
| Thomas Bätzler wrote:
>
> Brent Clark <bclark@eccotours.dyndns.org> asked:
>
>
>
> Generally, you can capture data from a RE with a pair
> of brackets around the relevant expression, and the
> data is then available in the variables $1..$9 for up
> to 9 pairs of brackets. You can also use the fact that
> a match in list context will return the catured fields
> as a list like this:
>
> open( IN, $csvfile ) or die "Can't open '$csvfile': $!";
>
> while( my $line = <IN> ){
>
> if( my( $filename ) = ( $line =~ m/^(.*?)\|/ ) ){
> print "file: $filename\n";
> } else {
> warn "line '$line' did not match\n";
> }
> }
>
> HTH,
> Thomas
>
I was thinking of using /^([^|]+)\|/ instead.
Also, is there any reason NOT to end the regex with /o to improve
performance?
| |
| John W. Krahn 2005-07-24, 8:29 pm |
| Tom Allison wrote:
>
> Also, is there any reason NOT to end the regex with /o to improve
> performance?
perldoc -q '/o'
Found in /usr/lib/perl5/5.8.6/pod/perlfaq6.pod
What is "/o" really for?
Using a variable in a regular expression match forces a
reāevaluation (and perhaps recompilation) each time the regular
expression is encountered. The "/o" modifier locks in the regex
the first time itās used. This always happens in a constant
regular expression, and in fact, the pattern was compiled into
the internal format at the same time your entire program was.
Use of "/o" is irrelevant unless variable interpolation is used
in the pattern, and if so, the regex engine will neither know nor
care whether the variables change after the pattern is evaluated
the very first time.
"/o" is often used to gain an extra measure of efficiency by not
performing subsequent evaluations when you know it wonāt matter
(because you know the variables wonāt change), or more rarely,
when you donāt want the regex to notice if they do.
For example, hereās a "paragrep" program:
$/ = āā; # paragraph mode
$pat = shift;
while (<> ) {
print if /$pat/o;
}
John
| |
| Bright True 2005-07-24, 8:29 pm |
| Hello ,
i think this would be a good way
#!/usr/bin/perl -w
use strict;
my $file = 'data.csv';
open(FILE,$file);
while(my $line = <FILE> ){
my @wanted = split(/\|/,$line);
print $wanted[0],"\n";
}
close(FILE);
On 7/21/05, Rob Coops <rcoops@gmail.com> wrote:
>
> Try the following (this is just a short example)
>
> #!/usr/bin/perl -w
>
> use strict;
>
> # Open the file
> open (IN, "<your_file.csv") or die "Oops unable to read the file error: "
> .
> $!;
> # Loop thru the fill till the bitter end
> while (<IN> ) {
> chomp; # Make sure there is no funny stuff at the end (not really needed
> in
> this example though)
> my ($temp) = $_ =~ /^(.*)\|.*$/; ($_ holds the line so we match the stuff
> we want to have and put it in $temp)
> print $temp . "\n"; # because I had no other things to do with this just
> print it to screen
> }
>
> close IN; # Close the file.
>
> Note that both warnings and strict is used in this application just as a
> for
> of good practice, at first it will make things more difficult but soon
> enough you will see that any good perl programer will not even start
> typing
> without enabeling both.
>
>
> On 7/21/05, Brent Clark <bclark@eccotours.dyndns.org> wrote:
>
>
|
|
|
|
|