reading from a file into an array in perl

S

Sooraj S

Hi,

I want to read all the data from a file into an array. But if there
is any "#" character in the file it should be replaced with "&"
character. How to store "$_" into an array?

This is my code:
my @tmp_arr;
open(IN,"<./ip_file");
while(<IN>)
{
if(/#/)
{
s/#/&/g;
}
-------------- //add ur code here
}
close(IN);
foreach(@tmp_arr)
{
print "$_";
}
 
J

Justin C

Hi,

I want to read all the data from a file into an array. But if there
is any "#" character in the file it should be replaced with "&"
character. How to store "$_" into an array?

This is my code:
my @tmp_arr;
open(IN,"<./ip_file");
while(<IN>)
{
if(/#/)
{
s/#/&/g;
}
-------------- //add ur code here
}
close(IN);
foreach(@tmp_arr)
{
print "$_";
}

I think you need <URL:http://oreilly.com/catalog/9780596001322>

Also, that 'if' is redundant. Just s/#/&/g without the 'if' around it,
if there isn't a '#' then there is no match and therefore no substitu-
tion.

There are a few other things that, around these parts at least, are
considered bad practice (and who am I to argue?), but, considering the
nature of the problem you have, I don't think it beneficial to point
them all out, but to suggest you read the book mentioned above.

Justin.
 
P

Peter Valdemar Mørch

        --------------                            //add ur code here}

Is this what you're looking for?

push @tmp_arr, $_;

Peter
 
J

Jürgen Exner

Sooraj S said:
I want to read all the data from a file into an array.

@arr = <$F>;

Or use File::Slurp
But if there
is any "#" character in the file it should be replaced with "&"
character.

AFAIR # isn't special in REs, so a simple
s/#/&/g;
should do. Or
tr/#/&/;
How to store "$_" into an array?

You assign the value of $_ to whatever position in the array you want
$arr[123] = $_;
This is my code:
my @tmp_arr;
open(IN,"<./ip_file");

You should use lexical file handles.
You should use the three-argument for of open()
You should always check for success of open()

open ($IN, '<', './ip_file') or die "Cannot open ./ip_file: $!";
while(<IN>)
{
if(/#/)
{
s/#/&/g;

There is no reason to hide the s/// inside of an if(). If there is no #
then the s/// just won't do anything anyway.
}
-------------- //add ur code here
}
close(IN);
foreach(@tmp_arr)

Where is this array coming from? You never declared it (are you using
warnings and strictures? You absolutely should!!!) and never defined it,
therefore it is empty and this loop will never be executed.
{
print "$_";

Please read "perldoc -q quoting":
What's wrong with always quoting "$vars"?

jue
 
P

Peter Valdemar Mørch

Where is this array coming from? You never declared it (are you using
warnings and strictures? You absolutely should!!!) and never defined it,
therefore it is empty and this loop will never be executed.

Uhm, didn't he in fact declare it explicitly?:

Sooraj said:
This is my code:
my @tmp_arr;
bla bla

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top