List in a different file used in the *.pl

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, I have read this a few times, I have tried it more.

I can't seem to get past that last detail that will help me understand how to call to an external file in a perl script, open the sucker, refer to it and use it.

Now I am clear on how to refer to it:

$list[0] or $list[$i] in a loop, $list[0][0] for lists with more bits to them, but how do I read it into the script in the first place, and what do I need to CHMOD the list in order to do it?

I think I need to CHOMP the list, too?

I don't know why I am stuck on this -- it seems so clear and yet I can't quite break the mental impasse that will make it ACTUALLY clear to me how it works and what I need to do to make it work.

Advice, sub-routines, tips, hints and links to tutorials all desparately welcome.

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

They have: 568 posts

Joined: Nov 1999

So you just want to know what chomp does?

it removes the \n (line break) from the end of each element.

They have: 99 posts

Joined: May 1999

Suzzane,
I am not clear on what you are referring to...
Do you have a text file that contains data you need to parse.....
If so, then use IO::File

$fh2 = IO::File->new("$listname", O_RDONLY);

while (<$fh2> ) {
$temp=$_;
chomp($temp); ##Strip that nasty \n
@templist = split(/;/, $temp); ### split line and assign to temp list

@templist2 = @cumlist;
@cumlist = (@templist2,@templist); #### add current to cumulative list

}

This example is to read from a text file a line at a time, split the line (assumes that you deliminate w/ ";" if not sub as necessary), and add to your cumulative list.

Hope this is what you were needing....if not, please post a clarification..

Tazman

PS: chmod the file in question to allow access for read rights by whatever class of user your server view a web process as....

Normally 444 is adequate.

[This message has been edited by tazman (edited 02 April 2000).]

They have: 1,587 posts

Joined: Mar 1999

that makes 3 of us, what did u say???

------------------
CLICK 4 some tested resources for making money $, hosting, and web promotions.
My Site got hacked, but i'm coming back?

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Strangely enough, I had to leave my beast (computer) last night and so I picked up the book...

Okay, here goes...

I have a multi-dimensional list that I need to access. It has two dimensions.

This is my solution, please post yea/nay!

code:

@images = ();

open (IMGLIST, "images.txt");
while (<IMGLIST> )
{
$TheImage = $_;
chomp ($TheImage);
@NewImage = (split/|/, $TheImage, 7);
push (@images, [@NewImage]);
}
[/code]

So according to the references, yes I do need to CHOMP (which I figured I did, but wasn't really very clear on how to go about it -- apparently, just CHOMP!)

My problem was the multi-dimensional aspect, and the actual reading of the file for use.

I assume (and do correct me if I am wrong) that at this point it should work, I don't need to print @images anywhere.

There won't be enough files to worry about a database, I don't think, but just in case, what is the max you find useful for a text-based list over a database?  Is up to 100 a good number before doing the switcheroo?

Also, it looks like I will have to read up on the :: stuff.  Thanks to Tazman, I am pretty sure you managed to answer most of the vague question (good going understanding me when I am confused)! And to Orpheus for the clear CHOMP bit.  

Thanks all!

  Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified 
Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

In an effort to troubleshoot what I thought was a problem with another part of the script, I printed @images and it returns this:

code:

ARRAY(0x80c2c44)ARRAY(0x80c3ba8)ARRAY(0x80c4fdc)ARRAY(0x80c5fbc) 
[/code]

Does that help someone troubleshoot what is wrong with the open/read/make a list program above??

Thanks!

Suzanne

Well, I figured out part of the problem - obviously the array means that it is returning an array (*hand slapping forehead*).  I am still not getting the text file parsed properly, but now I am worrying about the other part of the script.

Other opinions and options are welcome, please!



[This message has been edited by Suzanne (edited 02 April 2000).] 

They have: 850 posts

Joined: Jul 1999

Can you show us what the text file looks like?

------------------
L34RN |-|0\/\/ T0 5P34K L33T JU5T L1K3 4N 40L |-|4X0R
http://www.wiredstart.com : The Technology Start Page

They have: 161 posts

Joined: Dec 1999

tazman - there's NO need to use IO::File and cause a whole load of indirection. Filehandles are the preferred means of dealing with files.

Suzanne - your code is nearly correct.

code:

# your code
@images = ();
open (IMGLIST, "images.txt");
while (<IMGLIST>)
{
$TheImage = $_;
chomp ($TheImage);
@NewImage = (split /|/, $TheImage, 7);
push (@images, [@NewImage]);
}
[/code]

First, you should check the return value of open(), just to be sure it worked.  Next, you need to escape (backslash) the | inside a regular expression if you want to match a LITERAL |.  In regexes, | is an alternator.  Splitting on /|/ is the same as splitting on //, which means every character is a new element.

chomp() is a function that removes the ending substring of a string, if that substring happens to be the same as a particular variable, $/.  The default value of $/ is "\n", which means chomp() will remove a single ending newline from a scalar (or list of scalars).

code:
open IMG, "images.txt" or die "can't open images.txt: $!";
while (<IMG>) {
  chomp;  # no need to assign to another variable, and chomp() operates $_ as a default
  push @images, [ split /\|/, $_, 7 ];
}
close IMG;
[/code]

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve 
Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

You ever read someone's post and make that gutteral 'oh, my god, I finally get it!' sound that comes out like 'unk!'??

Thanks, that makes a BIG difference. As usual, it's the little details I trip over.

Much obliged, Japhy,

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, The file doesn't open and can't seem to be found...

Sigh.

The file is a text file -- formatted like this:

stuff|other stuff|more stuff|end of stuff
stuff1|other stuff 1|more stuff 1|end of stuff 1

Do I need to make it required file? Do I need to have it end with 1;?

Argh.

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

They have: 1,587 posts

Joined: Mar 1999

the split function?

i must say i'm just about totally lost on this one, i just don't understand and i'm confused, but what's new.

------------------
CLICK 4 some tested resources for making money $, hosting, and web promotions.
My Site got hacked, but i'm coming back?

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

They have: 161 posts

Joined: Dec 1999

The file's obviously not where you think it is. Check to make sure you're giving it the right path to the file.

A file doesn't need to end with a "1;" unless you're including the file via require() or use(). If you're merely opening it to read from or write to, it doesn't need to conform to any rules.

------------------
--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, so how do I tell the silly thing that it's in the same damn folder as the pl file?

Gddmn it I am going to get this included file thing straight, I know I will, but I am SO bloody frustrated. To test the rest of the script, I am just listing the whole list in the actual .pl file but I don't want to have it that way -- I want it separate.

Fairhousing -- split just breaks the file down into parts and feeds it into a list. So splitting fun|stuff|basketball|baseball would create @sports = ('fun', 'stuff', 'basketball', 'baseball'). I think. I hope.

What I want is to take:

fun|stuff|basketball|baseball
sad|stuff|funerals|death
happy|stuff|kittens|roses

and make one list:

@moods = (['fun', 'stuff', 'basketball', 'baseball'], ['sad', 'stuff', 'funerals', 'death'], ['happy', 'stuff', 'kittens', 'roses']);

But I have .gif and numbers in there, too, so I don't know if I need something extra to sort it out, because in my book, it says that actual numbers shouldn't be quoted, and I don't think anything is being quoted because I keep getting weird errors about the .gif which means I don't have 'something.gif' but just something.gif, which is wrong.

Ack!

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

[This message has been edited by Suzanne (edited 04 April 2000).]

They have: 1,587 posts

Joined: Mar 1999

this is the reason i try to make my own scripts when i can. because when i try to hack up one of those free ones it can be harder than making my own because i've got no idea what the other person was thinking when they made the script, hehe.

don't worry Suzanne, i'm confused here too.

------------------
CLICK 4 some tested resources for making money $, hosting, and web promotions.
My Site got hacked, but i'm coming back?

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, I hate to leave anyone hanging (like everyone here is just waiting for me to explain what I am doing -- ha!)

This is what finally worked:

What I did was type in the first couple of lines of the database into the script (list), work out the rest of the script, then generate the .txt file that I will be using for the database using this code:

code:


open(IMGOUT, ">images.txt") or die "die! die!";
$allimages = @images;
for ($i = 0; $i < $allimages; $i += 1)
    { print IMGOUT "$allimages[$i][0]|
$allimages[$i][1]|
$allimages[$i][2]|
$allimages[$i][3]|
$allimages[$i][4]|
$allimages[$i][5]|
$allimages[$i][6]|\n"}
[/code]

Looked for where the program was putting the !@##$% file, found it, then replaced the listing in the program with this:

code:

@images = ();
open (INIMG, "images.txt") or die "\n\n\n Oh no! We are missing the most important file!  Images.txt! \n\n\n";
while(<INIMG> )
    {
    chomp;
    @eachimage = split(/\|/, $_, 7);
    push (@images, [ @eachimage ]);
    }

[/code]

And that, my friends, worked like a dream.

So now I am off to build the next step!

Thanks for all the very helpful looks and hints, and yes, <b>Fairhousing</b> I ended up scr4pping everything, using my own logic and it worked out in a couple of hours, not the few weeks it was taking!  yay!

     Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

Edited to stop the scroll -- the first code doesn't really have a hard return at the end of each list element -- it's strung together in real life.     

[This message has been edited by Suzanne (edited 09 April 2000).] 

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.