Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Modifying a string

I am reading in a file which is in the following format:

./
./test1
./test2

I want to read in each value then store it into an array, but if the first value is "./", then I do not want to store it in the array.

How do I go about doing this?
So far, I used if ($line ne "./"), then do not store the string, but that does not seem to work.

0
jewee
Asked:
jewee
  • 3
  • 3
  • 2
  • +5
3 Solutions
 
jmcgOwnerCommented:
How about a line like:

next if substr( $line, 0, 2) eq './';

But perhaps I'm misunderstanding what you are trying to do. Please explain a little more, or provide more of the code that's not working, and we'll see what we can do.
0
 
bob_anastasiaCommented:


how about

if ($line =~ "^\.\/") {


}

this will only remove file that start with ./
0
 
jeweeAuthor Commented:
That wont work since each line  in the file starts off with ./

for example.

./
./test1
./test2
./test3
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bob_anastasiaCommented:
sorry,

if ($line =~ "^\.\/$") {


}

this will remove all that have JUST "./"
0
 
FishMongerCommented:
next if m!^\./$!

or maybe

next if length == 2;
0
 
bob_anastasiaCommented:
ps:

be sure to chomp the $line prior to doing this incase your server adds a carriage return at the end.

chomp($line);
0
 
TintinCommented:
open FILE, "file.list" or die "Can not open file.list $!\n";

while (<FILE>) {
  next unless /\w+/;
  chomp;
  push @filelist,$_;
}

0
 
mjcoyneCommented:
#!/usr/bin/perl -w
use strict;

my @array;
open (FH, "< filename.ext") or die "Can not open filename.ext: $!\n";

while (<FH>) {
  if (/^\.\/$/) {
    next;
  } else {
    chomp($_);
    push @array, $_;
  }
}
0
 
mjcoyneCommented:
Or, if you want to strip the ./ from the lines that are kept before pushing them into the array:

#!/usr/bin/perl -w
use strict;

my @array;
open (FH, "< filename.ext") or die "Can not open filename.ext: $!\n";

while (<FH>) {
  if (/^\.\/$/) {
    next;
  } else {
    $_ =~ s/\.\///;
    chomp($_);
    push @array, $_;
  }
}
0
 
jmcgOwnerCommented:
I'm still not sure what you're trying to do or what the problem is.

If 'chomp' doesn't solve the problem, please try explaining again what it is you want to do.
0
 
manav_mathurCommented:
I think whatever you are doing can probably be done better with File::Find. (or probably some other module). If you can please answer jmcg's question, we would be in a better position to help you
0
 
mjcoyneCommented:
I understood him to mean that he wanted to read a file and put the contents of the lines into an array, while skipping blank lines.  The only thing that makes this question a bit more esoteric than normal is the presence of the initial .\ characters...

What say you, jewee?
0
 
geotigerCommented:
If there are some tailing spaces, then you can

my @a = ();
open FILE, "file.list" or die "Can not open file.list $!\n";
while (<FILE>) {
    next if $_ =~ /^\.\/\s*$/;
    chomp;
    push @a,$_;
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now