Printing out from a flatfile db

They have: 40 posts

Joined: Oct 2001

If I have a flatfile db, with a line of pipe-separated values, say

v1|v2|v3|v4|v5|v6|v7|v8|v9|v10

normally I would use something like:

foreach $line (@raw_data)
{
chop($line);
($v1,$v2,$v3,$v4,$v5,$v6,$v7,$v8,$v9,$v10)=split(/\|/,$line);
print "$v1$v2$v3$v4$v5$v6$v7$v8$v9$v10";
}

to pull each variable out of the line and print it on a line of its own.

Is there a way to do it without declaring the variables in the line below chop($line); ? (Ie. "take each line, and basically replace the pipe-separator with a )

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this:

foreach $line (@raw_data) {
    chop($line);
    @tmp = split(/\|/,$line);
   
    foreach $field (@tmp) {
        print "$field<br>";
    }
    print "\n";
}
'

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 40 posts

Joined: Oct 2001

Thanks Mark,

This works fine...

They have: 601 posts

Joined: Nov 2001

That's a very slow way of reading from a file? Why load the file into memory before accessing it's contents? Wouldn't it be better to open the filehandle and just proces the information in one go?

I would of gone down the following route myself.

        open (INPUT, $file)         || die "can't open $file: $!";
        while (<INPUT>) {
            chomp;
            # do something with $_
        }
        close(INPUT)                || die "can't close $file: $!";
'
Rgds

- wil

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

*smacks self*
Ahh, yes.

They have: 601 posts

Joined: Nov 2001

LOL! Stop being so hard on yourself! Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.