Newbie seeks help using C commands in Perl 
Author Message
 Newbie seeks help using C commands in Perl

I've just started to learn Perl within the last week or so and am playing
around with files.  My specific problem involves the use of getc().  
After I've read in a file to an arry, how do I access specific characters
and put them in their own arrays to use them?

OK, example:

close (inFile);


Example:
123456789 95 userid

I want to get the first 9 digits into an array, the 2nd integer into
another array, and userid into a 3rd array so I can use them elsewhere in
the program.  I've been trying to use getc, but apparently I am doing
something wrong.  So, any help would be greatly appreciated.

Thanks,
Rachel



Fri, 14 Jan 2000 03:00:00 GMT  
 Newbie seeks help using C commands in Perl

Quote:

> I've just started to learn Perl within the last week or so and am playing
> around with files.  My specific problem involves the use of getc().
> After I've read in a file to an arry, how do I access specific characters
> and put them in their own arrays to use them?

> OK, example:
> open (inFile, $readme);

> close (inFile);


> Example:
> 123456789 95 userid

> I want to get the first 9 digits into an array, the 2nd integer into
> another array, and userid into a 3rd array so I can use them elsewhere in
> the program.  I've been trying to use getc, but apparently I am doing
> something wrong.  So, any help would be greatly appreciated.

> Thanks,
> Rachel

Perl does a lot of things with a lot less code than C.  I recommend
getting some documentation.  A good _short_ intro is at
http://www.cs.ccu.edu.tw/lab401/perl_related/perlcourse.html.

But to answer your question.... :)
Use a while loop for the equivalent of getc().  Use split to split
records in a file (untested):

open(FILE,"file");
while (<FILE>) {
        ($number[$i],$integer[$i],$userid[$i]) = split(/ /);
        $i++;

Quote:
}

Hope that helps,
Ben


Fri, 14 Jan 2000 03:00:00 GMT  
 Newbie seeks help using C commands in Perl

Quote:

> Perl does a lot of things with a lot less code than C.  I recommend
> getting some documentation.  A good _short_ intro is at
> http://www.cs.ccu.edu.tw/lab401/perl_related/perlcourse.html.

> But to answer your question.... :)
> Use a while loop for the equivalent of getc().  Use split to split
> records in a file (untested):

> open(FILE,"file");
> while (<FILE>) {
>         ($number[$i],$integer[$i],$userid[$i]) = split(/ /);
>         $i++;
> }

I would go with

#!/usr/bin/perl -w
use strict;

open(FILE, "<file") || die "cannot open file: $!\n";
while ( <FILE> )

close(FILE) || die "cannot close file: $!\n";

because
        -w
        use strict
        check that open() and close() work
        one list instead of three
        better support of tabs and multiple blanks

Your solution works, but I don't think that it is the best style for a
beginner.  Checking the return status of open() is critical for
beginners.

- doug



Fri, 14 Jan 2000 03:00:00 GMT  
 Newbie seeks help using C commands in Perl


Quote:
> I've just started to learn Perl within the last week or so and am playing
> around with files.  My specific problem involves the use of getc().  
> After I've read in a file to an arry, how do I access specific characters
> and put them in their own arrays to use them?

> OK, example:
> open (inFile, $readme);

> close (inFile);


> Example:
> 123456789 95 userid

A few points here:


           run you out of memory. Try using "while(<infile>)" instead.
        2. If you have a line from this file, say in $_, you do not
           need to use getc. getc's job is to read a single character
           from a stream. Since you have already read a whole line,
           you want something that will break up your fields. split
           was created for this purpose. e.g.:

                ($number1,$number2, $userid) = split(/\s+/,$_);

           Note the /\s+/, which is a regular expression (see perlre
           man-page). This regexp is used to find the delimeters
           between your data values. In this case, \s+ means any
           number (blut at least 1) white-space characters
           (e.g. space, tab, newline, vertical tab, etc).

You may find it useful to start off with the book, Learning Perl (2nd
edition, O'Reilly & assoc.).

                        -AJS



Fri, 14 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. (Q) Newbie seeks date-parsing Perl help

2. Newbie seeks help with PERL code

3. Newbie seeks help.....

4. Newbie humbly seeks help

5. Newbie seeks help......

6. Another newbie seeks help and assistance...

7. Newbie seeks help for random calls

8. XS newbie seeks help on memory violation

9. Newbie needs help- trying to submit command-line function to perl script via web

10. Newbie Help: Syntax of Perl Command

11. Hot novel by CS professor, includes perl code

12. PERL Newbie seeks answer with WWW script!

 

 
Powered by phpBB® Forum Software