From: Sooraj S on
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 "$_";
}
From: Justin C on
On 2010-06-30, Sooraj S <soorajspadmanabhan(a)gmail.com> wrote:
> 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.

--
Justin C, by the sea.
From: Peter Valdemar Mørch on
On Jun 30, 3:19 pm, Sooraj S <soorajspadmanab...(a)gmail.com> wrote:
>         --------------                            //add ur code here}

Is this what you're looking for?

push @tmp_arr, $_;

Peter
From: J�rgen Exner on
Sooraj S <soorajspadmanabhan(a)gmail.com> wrote:
>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
From: Peter Valdemar Mørch on
On Jun 30, 5:00 pm, J rgen Exner <jurge...(a)hotmail.com> wrote:
> 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 S wrote:
> This is my code:
> my @tmp_arr;
> bla bla

Peter