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

array reading problem

by jeah (Novice)
on Nov 23, 2008 at 08:00 UTC ( [id://725397]=perlquestion: print w/replies, xml ) Need Help??

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

Question (script at end of these notes):

* Why does my script give a result of '1' if I comment out line 6, using lines 4, 8 and 10?

* The script prints all my content correctly and completely if I comment out lines 4 and 8, using only lines 6 and 10.

The + signs in my script was automatically generated by this site, it's not part of my script. Thanks very much in advance for any help.

What I want to do:

Access a file's records whose individual attributes are delimited by pipes, the records themselves are separated by carriage returns/new line characters.

Desired Sequence:

1. Assign the file to a variable

2. Assign specific attributes of each record to various variables

3. For each record, pass the values in these variables into a T-SQL statement that updates/inserts various SQL Server tables.

Peripheral Notes:

- I'm running Perl on WinXP

- I already have the proper DBI module installed and code for the database connection and data manipulations

- The entire file's records are as follows (verbatim in content and format as the file's):

Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789

Vca|Wxr|384 Mkl Ln|Xillo|CrrntStt|CT|05506|1015567781|1015567782

- In my script (for testing item 1 only), the file's content is also hardcoded for testing (line 6), it includes the following to mimic the actual file's format: . "\n" . my script follows:

#!/usr/xyz/perl -w use strict; my @r = open(DATFH, 'C:\begperl\my scripts\cdata.txt') or die "File pr +ob: $! \n"; #my @r = "Bao|Voa|321 st||New York|NY|2120001356|9172804972" . "\n" . +"Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782"; close DATFH; print @r;

Replies are listed 'Best First'.
Re: array reading problem
by rdfield (Priest) on Nov 23, 2008 at 08:33 UTC
    You're assigning the result of the open to the array, rather than reading the file:
    open(DPATH,"..."); my @r = <DPATH>; ...

    rdfield

Re: array reading problem
by linuxer (Curate) on Nov 23, 2008 at 13:28 UTC

    As rdfield already mentioned, your problem is in the assignment of open()'s result to your array @r.

    When reading the file's content, remember that Perl doesn't remove the linebreaks automatically; that's up to you (use chomp() for that):

    # ... @r = <DATFH>; chomp @r;
    Instead of reading the complete file into an array (and into the memory) you can read linewise:
    #!/usr/bin/perl use strict; use warnings; my $file = 'data.txt'; open my $datfh, '<', $file or die "$file: open(ro) failed: $!\n"; while ( my $line = <$datfh> ) { chomp $line; my @fields = split /\|/, $line; # work with your data fields } close $datfh or die "$file: close(ro) failed: $!\n";

    update: paragraph tags and text added;
    update2: typo fixed; thanks johngg

      You have a small but significant typo in your code.

      while ( my $line <$datfh> ) {

      Should be

      while ( my $line = <$datfh> ) {

      Cheers,

      JohnGG

Re: array reading problem
by jeah (Novice) on Nov 24, 2008 at 05:15 UTC

    ah, so that's what it was, thanks very much everyone for pointing out the problems, really appreciate your quick feedback. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found