We have a routine on our website that needs to pull information from a text file. The file is a tab delimited file with double quotes surrounding the text. The routine needs to compare these values to another imported list of values that do not have the double quotes around the text.

I have been able to get the information imported to a hash but the double quotes are still there. Is there anyway to use the qw operator to accomplish this?

Recommended Answers

All 6 Replies

use strict;
use warnings;
my $other_list=('Hello,World,He,Can,See,Me');
my @other=split(/\,/,$other_list);
my %ref;
for (@other){
	$ref{$_}=1;
}
while(<DATA>){
	chomp;
	s/^\"//;
	s/\"$//;
	my (@comp)=split(/\"\,\"/);
	
	for (@comp){
		$ref{$_}=0 if(!$ref{$_});
		print "$_ is in other list\n" if($ref{$_});
	}
}
__DATA__
"Hello","World","I","Can","See","You"

Output:

Hello is in other list
World is in other list
Can is in other list
See is in other list

That's one way to do it. If you split on "," and remove beginning and end quotes.

You want simple one.
Check this out.

my @S=("hello","ben","foo",,"","bar","rose","world","foo",'loo');

foreach (@S)
{
	print(s/\",'*//,$_);
	print "\t";
	}




## OUT PUT

hello    ben     foo    bar     rose    world    loo

You want simple one.
Check this out.

my @S=("hello","ben","foo",,"","bar","rose","world","foo",'loo');

foreach (@S)
{
	print(s/\",'*//,$_);
	print "\t";
	}




## OUT PUT

hello    ben     foo    bar     rose    world    loo

In the above example there is nothing to remove. You will find that replacing print(s/\",'*//,$_); with a simple print; will give the same output.

It will, but it doesn't answer the question of removing quotes in a hash.

In the above example there is nothing to remove. You will find that replacing print(s/\",'*//,$_); with a simple print; will give the same output.

Well sorry to divert a little from here.

simple Print will not remove quotes in a hash my good friend.
I know you know that yourself. ;)
...

Well sorry to divert a little from here.

simple Print will not remove quotes in a hash my good friend.
I know you know that yourself. ;)
...

I do know that print; will not remove quotes from data stored in a hash, but I don't see any hash in your example. Maybe I'm missing the point but in your example I don't see how print(s/\",'*//,$_); does anything more that print; .

I think s/\",'*// says "remove all strings consisting of a double-quote followed by a comma followed by zero or more single-quotes." That won't do anything because the data probably won't contain strings of those characters in precisely that sequence. Perhaps you meant to define a character class and forgot the square brackets?

An example of removing quotes from data read from a tab-delimited file before storing that data in a hash would be something like

#!/usr/bin/perl
use strict;
use warnings;
#file2hash.pl

my %h; #Store each unique word from file as key in hash
while(<DATA>){
    chomp;
    my @words = split(/\t/, $_); #Store words in array.
    foreach(@words){
        s/"//g; #Remove double quotes before storing word in hash.
        undef($h{$_}); #Store word as key in hash. Value is not defined.
    }
}

foreach(keys %h){
    print "$_\n";
}

__DATA__
"here"	"is"	"a"
"tab-delimited"	"file"
"with"	"quotes"	"around"
"the"	"text"

Comparing these keys to another list in another hash could be done pretty much as in mitchems example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.