Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Passing Array to Function for search and Replace

by ikkon (Monk)
on Jan 08, 2007 at 18:30 UTC ( #593589=perlquestion: print w/ replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have come along way with this script and I am running into a few problems, first I have a group of variables I put into a array and pass the array to a function that based on currency searches the array item and should find characters and remove them, though I am not completely proficient with Regular expressions I am trying to learn more. here is my code for the searching function
my @fixCur =($hardwareValue1, $hardwareValue2); checkCurrency(@fixCur); sub checkCurrency{ for (@_){ if ($currency == "Euro (EUR)") { $_ =~ s/€//g; }elsif ($currency == "Japan, Yen(JPY)") { $_ =~ s// /g; }elsif ($currency == "United Kingdom, Pound(GBP)") { $_ =~ s// /g; } } }
Also after this pdf is created I am trying to display this to the browser, some have tried to explain to me but I have yet been able to undertand for some reason.
open(PDF, "LeasevsBuy.pdf") or die "could not open PDF [$!]"; binmode PDF; close(PDF); print "Content-Type: application/pdf\n"; print "Content-Length: " .length($output) . "\n\n"; print <PDF>;
thanks for any help!

Comment on Passing Array to Function for search and Replace
Select or Download Code
Replies are listed 'Best First'.
Re: Passing Array to Function for search and Replace
by Tanktalus (Canon) on Jan 08, 2007 at 18:43 UTC

    First, you want eq, not ==. eq is used for string comparisons, == is used for numeric comparisons.

    Second, strictly speaking, you aren't removing characters for JPY and GBP. You're replacing the character with a space. Do you intend to do that, or do you intend to remove it?

    Third, you probably want to put the characters in brackets ([]):

    I'm not sure if you intend the comma to be a character to remove. Note that there is a huge difference here - what you had only removed the characters when they appeared exactly as-is, in order, one after another. This one will remove all occurences of any character in brackets.

    Next, move the close(PDF); to after the print <PDF>. You can't read from a closed filehandle.

    Maybe what you really want is:

    open my $pdf, "<", "LeasevsBuy.pdf" or handleError($!); # you probab +ly want to print out an error page instead of just dying. binmode $pdf; $output = do { local $/; <$pdf> }; close $pdf; print "Content-Type: application/pdf\n"; print "Content-Length: " .length($output) . "\n\n"; print $output;
    Just guessing here.

    Hope that helps.

      thanks for the explination it makes sense, I am sure I am missing something important here, but even when using your updated version for the second part all i get is encrypted gumble, instead of the file opening in the browser, what am I doing wrong?
Re: Passing Array to Function for search and Replace
by ikegami (Pope) on Jan 08, 2007 at 18:45 UTC

    As for your second question,

    • You close PDF before you read from it.
    • You binmode when you read, but you don't when you write.
    • print while <PDF>; would use less memory.
    • What's $output?
    • It's better to use the 3-arg open over the 2-arg open to open files.
    my $file_name = "LeasevsBuy.pdf"; open(PDF, '<', $file_name) or die "could not open PDF \"$file_name\": $!\n"; binmode PDF; my $size = -s $file_name; print "Content-Type: application/pdf\n"; print "Content-Length: $size\n\n"; binmode(STDOUT); $/ = \4096; # Read 4k at at time. print while <PDF>; close(PDF); # Optional.

    Update: Overlooked that $output was used without being set. Fixed.

Re: Passing Array to Function for search and Replace
by ikegami (Pope) on Jan 08, 2007 at 18:37 UTC
    • @_ =~ s/.../.../g;
      should be
      $_ =~ s/.../.../g;
      or just

      $_ is aliased to the current member of @_ by the foreach loop.

    • == string
      should be
      eq string
      See perlop for the difference.

    checkCurrency($hardwareValue1, $hardwareValue2); sub checkCurrency{ for (@_) { if ($currency eq "Euro (EUR)") { s/€//g; }elsif ($currency eq "Japan, Yen(JPY)") { s// /g; }elsif ($currency eq "United Kingdom, Pound(GBP)") { s// /g; } } }

    Of course, the real problem is that you're not using Encode's decode to translate UTF-8 bytes into characters, and later Encode's encode to translate the characters into the proper encoding (or Encode's from_to to do it in one step).

      I have been trying to looking into the Encode, but was never sucessful, it would not print anything when I tried, since i have to get this working today, I didn't have alot of time to figure it out I spent a few hours on it but still couldn't wrap my mind around it.

        Say you want to convert from UTF-8 to iso-latin-1,

        use Encode qw( decode encode ); ... input ... foreach ($hardwareValue1, $hardwareValue2) { $_ = decode('UTF-8', $_); } ... do some work ... foreach ($hardwareValue1, $hardwareValue2) { $_ = encode('iso-latin-1', $_); } ... output ...

        If you just want to translate,

        use Encode qw( from_to ); ... input ... foreach ($hardwareValue1, $hardwareValue2) { $_ = from_to($_, 'UTF-8', 'iso-latin-1'); } ... output ...

        If a given char doesn't exist in iso-latin-1, you'll get a ? instead.

        Remember, when you read in something, it is encoded. Convert it to characters before working on it. When you need to write something, it needs to be encoded.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://593589]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2016-04-29 19:27 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.