Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

File Parsing

by Anonymous Monk
on Aug 22, 2004 at 00:43 UTC ( [id://384853]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Experts,

I am trying to parse my file and extract a particular word located in a specific place, and with that extracted word I would like to place it in another file (that to in a particular field). Are there any examples that any of you might have which I could make use of. Please do note that I am a beginner and getting my feet wet with perl.

Thanks a bunch

Replies are listed 'Best First'.
Re: File Parsing
by been42 (Curate) on Aug 22, 2004 at 01:10 UTC
    OK, are you saying that you are checking in a specific location in a file and extracting whatever word is there, or that you are always looking for the same word in the same place? Either way, as long as you're checking in one specific place, check out the documentation for seek. That should get you at least halfway where you want to go. Once we know what kind of word you're looking for, we can work out the other half.
      I am looking for a word which is always on the same line and at the sa +me position. And I want to then take that word and place it in a diff +erent file at a particular line at a particular place. Hope this helps
Re: File Parsing
by csuhockey3 (Curate) on Aug 22, 2004 at 06:08 UTC
    If by specific place you mean line of file, I suggest using Tie::File
Re: File Parsing
by mpeg4codec (Pilgrim) on Aug 22, 2004 at 20:51 UTC
    If you know what line the word is on and the file is in an array, you can work with that specific line. Let's say it's on line four and the file had been read into the array @file. To just work with line four, you'd have to work with the value $file[3]. Remember that Perl starts numbering line one as zero.

    As for the location of the word on that line, if you know which word (in terms of the number of the word) it is, you can again make use of an array. Again, working with line four:

    my @words = split(/\s/,$file[3]);

    Perl numbers the first word as word zero, and so forth. So, if it were say, the ninth word, the value would be stored in $words[8].

    If you don't know its position relative to other words, but do know that it's characters seven through ten on the line, for instance, you could make use of substr. Perl again starts the numbering at zero, so:

    my $word = substr($file[3], 6, 3); # The third argument to substr is the length of the string

    If this doesn't help, clarify it a bit, and I'll see what I can do. Good luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384853]
Approved by been42
Front-paged by been42
help
Chatterbox?
and the web crawler heard nothing...

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

      No recent polls found