Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: path is broken

by kilinrax (Deacon)
on Dec 06, 2000 at 23:53 UTC ( [id://45297]=note: print w/replies, xml ) Need Help??


in reply to path is broken

Don't use cat to read the file!

Seriously, you can do all of this very easily from within Perl itself. Here's an example of how:
#!/usr/bin/perl -w use strict; my $filename = "foo.txt"; open FILE, $filename or die "could not open $filename"; my @data = <FILE>; close FILE; print scalar @data;

Replies are listed 'Best First'.
Re: Re: path is broken
by gaspodethewonderdog (Monk) on Dec 07, 2000 at 00:03 UTC
    that's well and good... but what if the file is large? why not do basically the same thing except...
    use strict; my $filename = "foo.txt"; my $count; open FILE, $filename or die "could not open $filename"; while(<FILE>) { $count++; } # while close FILE; print $count;
    I imagine it should run about the same speed wise, but shouldn't require quite so much memory...
      Perl keeps track of the line number of the file you are in so you don't have to:
      perl -e "while(<>){};print $." somefile.txt
      Gotta love that $. variable :)

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        I particularly like this code, posted by Abigail on (I believe) comp.lang.perl.misc:
        perl -wlpe '}{*_=*.}{' <file>
        There's a lot of $. (<-- that dot is a regex =]) variables to love, yet I've only stumbled across a few of them, and even less explanations of what those variables do. Anyone know of a comprehensive listing of what variables are available to us in the $/./ (<-- that is a correct regex!) format and what they all do? I haven't noticed it in any of my books (I haven't read all of every book yet, so standard disclaimer of "i may be a fool" applies here).

        -marius
Re: Re: path is broken
by ashok (Sexton) on Dec 07, 2000 at 10:37 UTC
    Hi, I appreciate your help. But still I have one more problem. As I did explain, I am reading file names from a file. This file contains some history of each file in the entire source code. Like:
    Module: /org/trans/program files/dir1 /dir2/file1.cpp some flags some history
    So I pickup file name using reglar expression. My problem is when it breaks into two lines, like
    Module: /org/trans/program files/dir1/subdir1/subdir2/file1.cpp some flags some history
    In this case how to write my regular expression? I do not know in which line filename in a file breaks into two lines. The file I read is a big file and I can not keep into memory. Thanks again to you all. I am learning a lot. Ashok
      So if you're going through your file looking for ^Module and getting the what may be the filename;
      You may want to do a test if the file exists: (-e $filename) and then if it does not, grab the next line and tack it on.
      Though make sure you tack through the next Module line.
      Does it break on line length or on spaces? Consider that as well.
      I assume that what you're looking for here is the string '/org/trans/program files/dir1/subdir1/subdir2/file1.cpp'.
      Crafting a regex to extract the file name without having a format you can rely on is going to be tricky.
      However, if you are always going to be looking for a '.cpp' file, then the following will work:
      #!/usr/bin/perl -w use strict; my $data = join '', <DATA>; my ($filename) = ($data =~ m|^Module: (.*\.cpp)$|ms); $filename =~ s|\n||; print $filename; __DATA__ Module: /org/trans/program files/dir1/subdir1/subdir2/file1.cpp some flags some history

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45297]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found