Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting strings out of a text file.

Status
Not open for further replies.

WijnandW

Technical User
Feb 2, 2003
4
0
0
NL
Hello world,

As a big fan of the Magic the Gathering game I was trying to build a card database so I can search for specific cards.
I want to load all the cards from the spoilerlists into a mysql database.

The original text file is:

I already cut out the lines that aren't of any interest.

I want to know everything about every card so I can put it into the database. So I have to analyze every card.

This is the script I tried to use:

#!/usr/bin/perl -w

open (FH1,"le_spoiler_en.txt");

undef $/;
@cards = split(/\/145/, <FH1>, -1);
$/ = &quot;\n&quot;;



for( $i = 0; $i < scalar (@cards);$i++ )
{

$card= shift;
$card=$cards[$i];

$cardname= shift;
$cardname=$card;

$cardname=~ s/Card Name:\s+//gs;
$cardname=~ s/Card Color:.*Card \#:.*\d+//sg;
$cardname=~ s/\n//g;
$cardname=~ s/\t//sg;

$cardcolor=$card;
$cardcolor=~ s/Card Name:.*//g;
$cardcolor=~ s/Card Color:\s+//gs;
$cardcolor=~ s/Mana Cost:.*Card \#:.*\d+//sg;
$cardcolor=~ s/\n//sg;
$cardcolor=~ s/\t//sg;

print &quot;$cardname is of the color $cardcolor\n&quot;;
}

This should tell me the name of the cards and what color it is. It only just doesn't work as expected.

The output looks like:
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er
Gis of the color er

This can't be right, right?

What am I doing wrong? I just don't see it.
 
I'm not sure exactly what's wrong with your script, but the approach seems overly complicated to me. Try this version. It appears to work on the simple tests I ran.

-Bill

#!/usr/bin/perl

open FH1, &quot;<le_spoiler_en.txt&quot; || die &quot;can't open card file&quot;;

my $prevfield;
my %cardinfo;
my @allcards;
my @fields = ( 'Card Name', 'Card Color', 'Mana Cost', 'Type & Class',
'Pow/Tou', 'Card Text', 'Flavor Text', 'Artist', 'Rarity',
'Card #' );

while (<FH1>)
{
chomp;
# ignore blank lines
next if /^$/;
my $line = $_;
# line starting with Tab are continuation of previous field
if ($line =~ /^\t/)
{
$line =~ s/^\t+//;
$cardinfo{$prevfield} .= $line;
next;
}
# everything else has field:\t+contents
my ($fn, $contents) = split(/:/, $_, 2);
$prevfield = $fn;
$contents =~ s/^\t+//;
$cardinfo{$fn} = $contents;
if ($fn eq 'Card #')
{
# last field for this entry
# could either save to database now, or just collect
# them all in an array
push @allcards, [ @cardinfo{@fields} ];
undef %cardinfo;
}
}

close FH1;

# debug - print results
foreach $c (@allcards)
{
foreach $k (@$c)
{
print &quot;$k\n&quot;;
}
print &quot;-----\n&quot;;
}

 
If you just want to print the name and color then you can simply do
Code:
open (FH1,&quot;le_spoiler_en.txt&quot;) or die &quot;Can't open card file: $!&quot;;
$/ = &quot;\n\n&quot;; # Change to 'record' mode instead of line mode

while (<FH1>) {

if ( /Card Name:\s+(.*?)\s*\nCard Color:\s+(.*?)\s*\n/ ) {

        my ($cardname, $cardcolor) = ($1, $2);
        print &quot;$cardname is of the color $cardcolor\n&quot;;
    }
}
close (FH1);
jaa
 
Well, thank you guys, I really appreciate your help.

I already found out what was wrong, the file was full with tabs and ^M's. When chopped those out it all worked fine.

But you guys made me think, my approach is far too difficult. But it was the only way I knew how to do the job. The script does the job now, but I am thinking of rewriting it to make it
work more efficient.

Thanks guys, I learned a lot from you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top